javascript 鼠标悬停时停止幻灯片放映
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21454318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop slideshow on mouseover
提问by SmalliSax
I have a slideshow that shifts between 3 different images that are separated in a DIV
tag. I want when you hover the mouse over the slideshow it should stop and when you take the mouse off the slideshow it should continue roll through it.
我有一个幻灯片,可以在DIV
标记中分隔的 3 个不同图像之间切换。我希望当您将鼠标悬停在幻灯片上时,它应该停止,当您将鼠标从幻灯片上移开时,它应该继续滚动。
The code is here:
代码在这里:
function slideSwitch() {
var $active = $('#slideshow3 div.active3');
if ($active.length == 0 ) $active = $('#slideshow3 div:last');
var $next = $active.next().length ? $active.next() : $('#slideshow3 div:first');
$active.addClass('last-active3')
.animate({opacity : 0.0}, 1000);
$next.css({opacity: 0.0})
.addClass('active3')
.animate({opacity: 1.0}, 1000, function() {
$active.removeClass('active3 last-active3');
});
}
I tried for starters do something like this:
我尝试初学者做这样的事情:
$("#slideshow3").mouseover(function(){
$(this).stop();
return false;
});
But the slideshow did not even stop, so I'm definitely not targeting it correctly or putting the code in at the right place.
但是幻灯片甚至没有停止,所以我绝对没有正确定位它或将代码放在正确的位置。
Can you give me some suggestions ?
你能给我一些建议吗?
Thank you!
谢谢!
回答by Irvin Dominin
You can store your interval reference in a variable, then when you hover the image stop the interval and when you exit start it.
您可以将您的区间引用存储在一个变量中,然后当您悬停图像时停止该区间,并在退出时启动它。
Code:
代码:
var theInterval;
function startSlide() {
theInterval = setInterval(slideSwitch, 5000);
}
function stopSlide() {
clearInterval(theInterval);
}
$(function () {
startSlide();
$('#slideshow DIV').hover(function () {
stopSlide();
}, function () {
startSlide();
})
});