javascript - 覆盖/更改背景的问题

标签 javascript html

我有一个包含 3 个按钮的页面。该页面有一个 gif 作为默认背景。 Button 1 应该使背景变黑。 Button 2 应将背景更改回 gif。 当您将鼠标悬停在 按钮 3 上时,背景会闪烁 红色绿色

加载后我可以将背景更改为黑色,但不能变回gifButton 3 可以使用 gifblack 背景,但使用它后我无法将背景更改为 black 使用 button 1 。 (并且 button 2 仍然不起作用) 如何让这 3 个按钮发挥作用并更改背景?

var myHoverInterval = null;

function switchfunction() {
  if (document.body.style.background === 'red') {
    document.body.style.background = 'green';
  } else {
    document.body.style.background = 'red';
  }
}

window.onload = function() {
  // Get references to the DOM elements you'll work with
  var bdy = document.body;
  var btn = document.getElementById("changeBackgroundButton");
  var btn2 = document.getElementById("changeBackgroundBackButton")
  var btn3 = document.getElementById("trippyBackground")

  // Set up the button to have a click event handler:
  btn.addEventListener("click", function() {
    /* Just remove the image and the page will revert to the previously set color */
    bdy.style.backgroundImage = "url('')";
  });
  btn.style.color = "red";
  btn2.addEventListener("click", function() {
    /* Just remove the image and the page will revert to the previously set color */
    bdy.style.backgroundImage = "url(https://media.giphy.com/media/ko7twHhomhk8E/giphy.gif) no-repeat center center fixed";
  });
  btn3.addEventListener("mouseover", function() {
    if (myHoverInterval != null) {
      return;
    }
    myHoverInterval = setInterval(function() {
      switchfunction();
    }, 500);
  });
  btn3.addEventListener("mouseout", function() {
    if (myHoverInterval != null) {
      clearInterval(myHoverInterval);
      myHoverInterval = null;
    }
  });

}
<!DOCTYPE html>
<html>

<head>
  <title></title>
  <style>
    body {
      background: url(https://media.giphy.com/media/ko7twHhomhk8E/giphy.gif) no-repeat center center fixed;
      background-size: cover;
      background-color: black;
    }
  </style>
</head>

<body>
  <p>
    <button id="changeBackgroundButton" style="color:white; font-size: 150%;background-color :lightblue">Click this button if the page is slow, it's probably due to the background</button> <br>
    <button id="changeBackgroundBackButton" style="color:white; font-size: 150%;background-color :lightblue">Try to load the background again</button> <br>
    <button id="trippyBackground" style="color:white; font-size: 150%;background-color :lightblue">trolololol?</button>
  </p>

</body>

</html>

最佳答案

您的代码中存在一些问题。

    CSS 中的
  • background 是所有背景属性的通用属性。在您的情况下,最好不要使用它并单独指定每个属性,例如background-imagebackground-color
  • mouseout 事件结束时,您必须告诉页面恢复到原始背景。

这是您的代码的改进版本,没有任何错误(我希望如此)。

var myHoverInterval = null;

function switchfunction() {
  //You must remove the background image before changing bgcolor to red or green
  //Use backgroundColor instead of background
  document.body.style.backgroundImage = "url('')";
  if (document.body.style.backgroundColor === 'red') {
    document.body.style.backgroundColor = 'green';
  } else {
    document.body.style.backgroundColor = 'red';
  }
}
window.onload = function() {
    // Get references to the DOM elements you'll work with
    var bdy = document.body;
    var btn = document.getElementById("changeBackgroundButton");
    var btn2 = document.getElementById("changeBackgroundBackButton");
    var btn3 = document.getElementById("trippyBackground");
    var img; // This variable will be used to remember the background before the hover events

    // Set up the button to have a click event handler:
    btn.addEventListener("click", function() {
      /* Just remove the image and the page will revert to the previously set color */
      bdy.style.backgroundImage = "url('')";
    });
    btn.style.color = "red";
    btn2.addEventListener("click", function() {
      /* Just remove the image and the page will revert to the previously set color */
      // Use backgroundImage for the GIF
      bdy.style.backgroundImage = "url(https://media.giphy.com/media/ko7twHhomhk8E/giphy.gif)";
    });
    btn3.addEventListener("mouseover", function() {
      if (myHoverInterval != null) {
        return;
      }
      // Store current bgImage in variable
      img = bdy.style.backgroundImage;
      // Call func straight away once before the interval so it takes effect straight away
      switchfunction();
      myHoverInterval = setInterval(function() {
        switchfunction();
      }, 500);
    });
    btn3.addEventListener("mouseout", function() {
      if (myHoverInterval != null) {
        clearInterval(myHoverInterval);
        myHoverInterval = null;
        // Set the background image and color back to what they were
        bdy.style.backgroundColor = 'black';
        bdy.style.backgroundImage = img;
      }
    });
}
body {
  background-image: url(https://media.giphy.com/media/ko7twHhomhk8E/giphy.gif);
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  background-opacity: 0.6;
  background-color: black;
}
<body>
  <p>
    <button id="changeBackgroundButton" style="color:white; font-size: 150%;background-color :lightblue">Click this button if the page is slow, it's probably due to the background</button>
    <br>
    <button id="changeBackgroundBackButton" style="color:white; font-size: 150%;background-color :lightblue">Try to load the background again</button>
    <br>
    <button id="trippyBackground" style="color:white; font-size: 150%;background-color :lightblue">trolololol?</button>
  </p>
</body>

关于javascript - 覆盖/更改背景的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43782280/

相关文章:

javascript - 缺少 : after property id - javascript

javascript - 使用 JS 将属性应用于 SVG 提示 'this.textBox.style is undefined' 。为什么?

javascript - 幻灯片显示下面的另一张幻灯片

javascript - php发送脚本后引导模式在同一页面中打开

html - 具有固定像素间距的流体网格布局

php - 单击时使用javascript更改输入按钮样式

javascript - 拆分字符串

javascript - 如何将分层 csv(分号分隔)转换为多维数组

html - 使用 CSS first-child 选择第一个 H2

html - Bootstrap ~3.7 和最新 Bootstrap 之间简单代码的样式问题 (4)