我有一个图像滑块,每5秒更换一次图像,但是当我将鼠标悬停在div img上时,我希望滑块暂停。有没有办法在悬停时暂停'theRotator'功能,然后在鼠标离开div后重新启动它?谢谢大家!
function theRotator() {
//Set the opacity of all images to 0
$('div.rotator ul li').css({opacity: 0.0});
//Get the first image and display it (gets set to full opacity)
$('div.rotator ul li:first').css({opacity: 1.0});
//Call the rotator function to run the slideshow,
setInterval('rotate()',5000);
}
$(document).ready(function() {
//Load the slideshow
theRotator();
$('div.rotator').fadeIn(1000);
$('div.rotator ul li').fadeIn(1000); // tweek for IE
});
答案 0 :(得分:1)
您需要检索setInterval
函数返回的间隔句柄。另外,我使用mouseenter
和mouseleave
,因为它们更适合您想要的功能。
function theRotator() {
//Set the opacity of all images to 0
$('div.rotator ul li').css({opacity: 0.0});
//Get the first image and display it (gets set to full opacity)
$('div.rotator ul li:first').css({opacity: 1.0});
//Call the rotator function to run the slideshow,
window.rotatorInterval = setInterval(rotate,5000);
}
$(document).ready(function() {
//Load the slideshow
theRotator();
$('div img').mouseenter(function(){
clearInterval(window.rotatorInterval);
}).mouseleave(function() {
window.rotatorInterval = setInterval(rotate, 5000);
});
$('div.rotator').fadeIn(1000);
$('div.rotator ul li').fadeIn(1000); // tweek for IE
});
答案 1 :(得分:0)
您可以执行类似
的操作var rotate = true;
$('img').on({
mouseenter: function() {
rotate = false;
},
mouseleave: function() {
rotate = true;
}
});
并检查rotate()
功能
答案 2 :(得分:0)
您只需要在鼠标悬停时清除超时,然后在mouseout上重新创建它,如下所示:
var interval;
function theRotator() {
$('div.rotator ul li').css({opacity: 0.0});
$('div.rotator ul li:first').css({opacity: 1.0});
interval = setInterval(rotate, 5000);
}
$(document).ready(function() {
theRotator();
$('div.rotator').fadeIn(1000);
$('div.rotator ul li').fadeIn(1000); // tweek for IE
$('div.rotator ul li').hover(function() {
clearInterval(interval);
},
function() {
interval = setInterval(rotate, 5000);
});
});