我创建了一个带有W3.CSS's slideshow documentation的简单幻灯片。您可以在此 JS Fiddle上看到代码和幻灯片。
除了在“mouseleave”事件中幻灯片不会恢复时,一切正常。
$(document).ready(function(){
$("#slideshow").mouseenter(function () {
clearInterval(setInterval);})
.mouseleave(function(){setInterval(function(){
slideIndex++; currentDiv(slideIndex);}, 1000);});
});
我不知道如何使用我的代码重置间隔时间。有没有人愿意帮助我?
这是我的总代码:
<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<style>
.mySlides {display:none}
.w3-left, .w3-right, .w3-badge {cursor:pointer}
.w3-badge {height:13px;width:13px;padding:0}
</style>
<body>
<div class="w3-content w3-display-container" style="max-width:800px"
id="slideshow" onmouseleave="showDivs()">
<div class="mySlides w3-container w3-xlarge w3-red w3-card-4">
<h1><b>Slide 1</b></h1>
</div>
<div class="mySlides w3-container w3-xlarge w3-blue w3-card-4" >
<p>Slide 2</p>
</div>
</div>
<div class="w3-center w3-container w3-section w3-large w3-text-white w3-
display-bottommiddle" style="width:100%">
<div class="w3-left w3-hover-text-khaki" onclick="plusDivs(-1)">❮
</div>
<div class="w3-right w3-hover-text-khaki"onclick="plusDivs(1)">❯
</div>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white"
onclick="currentDiv(1)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white"
onclick="currentDiv(2)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white"
onclick="currentDiv(3)"></span>
<span class="w3-badge demo w3-border w3-transparent w3-hover-white"
onclick="currentDiv(4)"></span>
<script>
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
showDivs(slideIndex += n);
}
function currentDiv(n) {
showDivs(slideIndex = n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
if (n > x.length) {slideIndex = 1}
if (n < 1) {slideIndex = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" w3-white", "");
}
x[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " w3-white";
}
var setInterval = setInterval(function(){slideIndex++;
currentDiv(slideIndex);}, 1000);
$(document).ready(function(){
$("#slideshow").mouseenter(function () {clearInterval(setInterval);})
.mouseleave(function(){setInterval(function(){slideIndex++;
currentDiv(slideIndex);}, 1000);});
});
</script>
</body>
</html>
答案 0 :(得分:1)
您没有正确建立对计时器的引用,因此清除它并不起作用。
不要将变量命名为函数,对象,关键字或方法的名称:
var setInterval = setInterval(function(){slideIndex++; currentDiv(slideIndex);}, 1000);
因为它可以覆盖/禁用该名称提供的本机功能。
$(document).ready(function(){
var timer = null; // This will hold a reference to the timer
$("#slideshow").mouseenter(function () {
clearInterval(timer); // It's the timer reference you need to clear
})
.mouseleave(function(){
// Now, you have to establish the timer reference
timer = setInterval(function(){
slideIndex++; currentDiv(slideIndex);
}, 1000);
});
});