2017-05-04 10 views
3

3つのボタンがあるページがあります。ページにはデフォルトの背景としてgifがあります。 Button 1は、背景を黒くする必要があります。 Button 2は、バックグラウンドをgifに戻す必要があります。 button 3にカーソルを合わせると、背景がredと​​に点滅します。背景の上書き/変更に関する問題

ロード後、私は背景をblackにすることができますが、gifに戻ることはできません。 Button 3は、gifまたは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>

答えて

1

... 
    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"; 
    }); 
... 

も私のペンを参照してください。

  • backgroundは、すべての背景プロパティの汎用プロパティです。あなたの場合、おそらくそれを使用せず、個々のプロパティを個別に指定するのが最善です。 background-imageおよびbackground-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

あなたは、複数のスタイル属性を混合し、前の1をクリアしていません。 style.backgroundのみを使用することをお勧めします。これにより、以前の値が常にクリアされます。あなたのコード内のいくつかの問題がありhttps://codepen.io/anon/pen/NjvmYv

関連する問題