如何在hover时添加暂停(jquery)
这是代码:
$(document).ready(function(){ $('#testimonials .slide'); setInterval(function(){ $('#testimonials .slide').filter(':visible').fadeOut(1000,function(){ if($(this).next('li.slide').size()){ $(this).next().fadeIn(2000); } else{ $('#testimonials .slide').eq(0).fadeIn(2000); } }); },1000); });
这适用于ul列表,并希望在hover时添加暂停。 有什么建议。 ?
您需要保存setInterval
返回的对象,然后可以将其传递给clearInterval
以阻止它再次发生。
这里有一些示例代码可以让您获得类似于以下内容的代码:
$(document).ready(function(){ var slider = function() { $('#testimonials .slide').filter(':visible').fadeOut(1000,function(){ if($(this).next('li.slide').size()){ $(this).next().fadeIn(2000); } else{ $('#testimonials .slide').eq(0).fadeIn(2000); } }); }; // save an object so that I can start and stop this as we go var interval = setInterval(slider, 1000); // when the user hovers in, clear the interval; if they hover out, // restart it again $('#testimonials .slide').hover(function() { clearInterval(interval); }, function() { interval = setInterval(slider, 1000); }); });
请注意,我不清楚用户究竟是为了获得停止间隔的原因,因此我假设你想要$('#testimonials .slide')
来做到这一点。 如果没有,只需将该部分更改为您需要的任何部分。