2017-05-04 91 views
3

我有一个包含3个按钮的页面。该页面的默认背景为gifButton 1应使背景变黑。 Button 2应该将背景更改回gif。 当您将鼠标悬停在button 3上时,背景闪烁red和​​。覆盖/更改背景的问题

加载后,我可以将背景设置为black,但不能返回到gifButton 3作品不管gifblack背景,但使用它后,我不能使用button 1将背景更改为black。 (并且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>

回答

1

有代码中的几个问题。

  • background在CSS中是所有背景属性的通用属性。在您的情况下,最好不要使用它并单独指定每个属性,例如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>

0

你是混合多种样式属性,而不是清除前一个。我建议只使用style.background这确保以前的值总是被清除。

... 
    btn2.addEventListener("click", function() { 
    bdy.style.background = "url('https://media.giphy.com/media/ko7twHhomhk8E/giphy.gif') no-repeat center center fixed"; 
    bdy.style.backgroundSize = "cover"; 
    bdy.style.backgroundColor = "black"; 
    }); 
... 

参见我的笔:https://codepen.io/anon/pen/NjvmYv