Javascript 在鼠标悬停时启动和重复功能,在鼠标离开时停止重复



我在鼠标悬停时成功启动了一个动画,但在鼠标离开时无法停止:

开始动画:

var executed = false; // to execute only once on repeat hover
var startslider = {};
$('#imgBx').mouseover(function(){
if (!executed) { 
var startslider = setInterval(function(){
executed = true;  
rotateSlide();
}, 1000);
} // end if executed
}); // end mouseover

停止动画:

$('#imgBx').mouseleave(function(){
clearInterval(startslider);
});

您正在重新声明全局变量"startslider"。

从mouseover事件侦听器中的变量中删除var。

startslider = setInterval(function(){

最新更新