我试图让显示按钮带回 h1 标签,但它在达到 opacity: 0.1 后停止,而不是一直持续到 opacity: 1。
我已经尝试在 Firebug 上调试几个小时,但似乎无法破解它。请帮忙。
<!DOCTYPE html>
<html>
<head>
<title>Flesh and Bone</title>
</head>
<body>
<h1>Flesh and Bone</h1>
<button id="fade">Fade</button>
<button id="show">Bring Back</button>
<h2>The Killers</h2>
<script>
var el = document.querySelector("h1");
var fade = document.getElementById("fade");
var show = document.getElementById("show");
var fader = function () {
el.style.opacity = 1;
var timer = setInterval(function (){
el.style.opacity -= 0.1;
if (el.style.opacity == 0) {
clearInterval(timer);
}
}, 40);
}
var shower = function () {
el.style.opacity = 0;
var timer = setInterval(function (){
el.style.opacity += 0.1;
if (el.style.opacity == 1) {
clearInterval(timer);
}
}, 40);
}
fade.onclick = fader;
show.onclick = shower;
</script>
</body>
</html>