元素悬停时的暂停功能
Pause function on element hover
我有一个图像滑块,它每5秒更改一次图像,但我希望当我将光标悬停在div img上时,滑块会暂停。有没有办法在悬停时暂停"旋转器"功能,然后在鼠标移出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
});
您需要检索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
});
您只需要清除mouseover上的超时,然后在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);
});
});
你可以做一些类似的事情
var rotate = true;
$('img').on({
mouseenter: function() {
rotate = false;
},
mouseleave: function() {
rotate = true;
}
});
并在rotate()
函数中检查该标志
相关文章:
- Facebook股票计数,暂停直到回调功能结束
- 是否有“”的键盘快捷键;暂停动画”;Chrome开发工具中的功能?(F12)
- 暂停和取消暂停功能中的音频
- 故意暂停执行功能
- 停止/暂停/禁用第三个功能的 2 个功能
- 使用一个功能播放/暂停多个视频
- 音轨 JS 功能无法播放或暂停音频
- 我如何使因果报应;s的自动监视功能在运行测试前暂停
- 如何向正在运行的setInterval添加暂停/播放功能
- 禁用单击Fotorama滑块上的暂停功能
- 在另一个功能完成时暂停功能
- 为滑块添加暂停功能
- 元素悬停时的暂停功能
- 用于音频的 Flash HTML5 画布条件.暂停和恢复功能
- 我将如何获取此.scrollTop代码并对其进行修改以在鼠标悬停或滚动时暂停其功能
- 如何在父iframe中暂停html5音频,该功能来自子框架
- 增加了一个暂停功能,不能工作.有人知道解决方法吗?
- Javascript滑动条暂停功能在悬停
- OnChange -当shift或control键按下时暂停功能
- 具有暂停功能的JS浏览器,请帮忙