jQuery 悬停时暂停功能?
我有一个 jQuery/JS 函数,它使用 setInterval 来循环浏览我拥有的一些图像幻灯片。它只是每 5 秒翻转一次......
现在我希望当我的鼠标悬停在它上面时它会暂停。我该如何在 setInterval 函数上执行此操作?
var current = 1;
function autoAdvance() {
if (current == -1) return false;
jQuery('#slide_menu ul li a').eq(current % jQuery('#slide_menu ul li a').length).trigger('click', [true]);
current++;
}
// The number of seconds that the slider will auto-advance in:
var changeEvery = jQuery(".interval").val();
if (changeEvery <= 0) {
changeEvery = 10;
}
var itvl = setInterval(function () {
autoAdvance()
}, changeEvery * 1000);
I have a jQuery/JS function that is using setInterval
to loop through some image slides I have. It just flips through every 5 seconds...
Now I want it to pause if my mouse is hovered over it. How do I go about doing that on the setInterval function?
var current = 1;
function autoAdvance() {
if (current == -1) return false;
jQuery('#slide_menu ul li a').eq(current % jQuery('#slide_menu ul li a').length).trigger('click', [true]);
current++;
}
// The number of seconds that the slider will auto-advance in:
var changeEvery = jQuery(".interval").val();
if (changeEvery <= 0) {
changeEvery = 10;
}
var itvl = setInterval(function () {
autoAdvance()
}, changeEvery * 1000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设
interval
是在外部作用域中定义的,这样的事情就可以工作:Something like this would work assuming
interval
is defined in an outer scope:示例: http://jsfiddle.net/Zq7KB/3/
我重用了我编写的一些旧代码前几天有一个问题,但我认为这并不重要。诀窍是将间隔存储在后台保存的变量中。然后,当您将鼠标悬停在容器上时,清除间隔。当您将鼠标悬停在容器外时,重新设置间隔。为了更好地了解其工作原理,请将这些
5000
更改为1000
,以便更快地通过测试。希望这有帮助。
Example: http://jsfiddle.net/Zq7KB/3/
I reused some old code I wrote for a question the other day, but I figured it didn't matter that much. The trick is to store your interval in a variable that you keep in the background. Then, when you hover over the container, clear the interval. When you hover out of the container, re-set the interval. To get a better feel of how this works, change those
5000
s to1000
s so it passes more quickly for testing.Hope this helps.