gpt4 book ai didi

javascript - 鼠标悬停时停止幻灯片放映

转载 作者:行者123 更新时间:2023-11-30 17:38:57 25 4
gpt4 key购买 nike

我有一个幻灯片,它在 DIV 标签中分隔的 3 个不同图像之间切换。我希望当您将鼠标悬停在幻灯片上时它应该停止,当您将鼠标从幻灯片上移开时它应该继续滚动。

代码在这里:

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');
});
}

我试着让初学者做这样的事情:

$("#slideshow3").mouseover(function(){
$(this).stop();
return false;
});

但幻灯片甚至没有停止,所以我绝对没有正确定位它或将代码放在正确的位置。

你能给我一些建议吗?

谢谢!

最佳答案

您可以将间隔引用存储在一个变量中,然后当您将鼠标悬停在图像上时停止间隔,并在您退出时启动它。

代码:

var theInterval;

function startSlide() {
theInterval = setInterval(slideSwitch, 5000);
}

function stopSlide() {
clearInterval(theInterval);
}

$(function () {
startSlide();
$('#slideshow DIV').hover(function () {
stopSlide();
}, function () {
startSlide();
})
});

演示:http://jsfiddle.net/mucv5/

关于javascript - 鼠标悬停时停止幻灯片放映,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21454318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com