javascript - .mouseleave 之后幻灯片放映间隔未重置

标签 javascript jquery html slideshow w3.css

我用 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)">&#10094;
</div>
<div class="w3-right w3-hover-text-khaki"onclick="plusDivs(1)">&#10095;
</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> 

最佳答案

您没有正确建立对计时器的引用,因此清除它不起作用。

不要将变量命名为函数、对象、关键字或方法的名称:

var setInterval = setInterval(function(){slideIndex++; currentDiv(slideIndex);}, 1000);

因为它可以覆盖/禁用该名称提供的 native 功能。

$(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);
  });
});

关于javascript - .mouseleave 之后幻灯片放映间隔未重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48573442/

相关文章:

javascript - 保护多个范围时,Google Apps 脚本运行时间非常慢

javascript - 在附加之前修改元素位置和旋转

javascript - UIB 选项卡集不起作用

php - 通用分析不跟踪导致弹出窗口的链接

php - 如何从 MySQL 数据库检索 BLOB 格式的图像并在 html <img> 标签中显示?

javascript - 如何在 Ionic/Angular 项目中设置 Anime JS?

javascript - 为什么我的 javascript regex.test() 会给出交替的结果

javascript - 如果单击 img src 包含特定文本

javascript - 当另一个 span 元素包含特定文本时删除同级 div

javascript - 模块导出多个类