[英]Pause infinite loop made with jQuery
I'm using Infinite Loop function which is a bit modified for my needs: 我正在使用无限循环功能 ,可根据需要进行一些修改:
$(document).ready(function() {
var NewsRotator = {init: function()
{
var numberOfTitles = $('.NewsRotator li').length;
//set current item
var currentTitle = 0;
//show first item
$('.NewsRotator li').eq(currentTitle).fadeIn(1000); //initial fade-in
//loop through the items
var infiniteLoop = setInterval(function(){
$('.NewsRotator li').eq(currentTitle).fadeOut(100); //current item fade-out time
if(currentTitle == numberOfTitles -1){
currentTitle = 0;
}else{
currentTitle++;
}
$('.NewsRotator li').eq(currentTitle).fadeIn(900); //next item fade-in time
}, 3000); //interval between items
}
};
NewsRotator.init();
});
Here is the working example on jsFiddle 这是jsFiddle上的工作示例
What I want to do is to set an animation pause on mouse over and resume it when the mouse leaves. 我要做的是在鼠标悬停时设置动画暂停,并在鼠标离开时恢复播放。
Thank you! 谢谢!
Here's the method for setting a global flag to pause and play on hover, along with a jsFiddle . 这是将全局标志设置为在悬停时暂停和播放的方法,以及jsFiddle 。
var NewsRotator = (function(){
var numberOfTitles = $('.NewsRotator li').length,
currentTitle = 0;
return {
flag : true,
init : function(){
$('.NewsRotator').on({
mouseenter : function(){
NewsRotator.flag = false;
},
mouseleave : function(){
NewsRotator.flag = true;
}
});
$('.NewsRotator li').eq(currentTitle).fadeIn(1000);
infiniteLoop = setInterval(function () {
if (NewsRotator.flag == true){
$('.NewsRotator li').eq(currentTitle).fadeOut(100);
if (currentTitle == numberOfTitles - 1) {
currentTitle = 0;
} else {
currentTitle++;
}
$('.NewsRotator li').eq(currentTitle).fadeIn(900);
} else {
return false;
}
}, 3000);
}
};
}());
$(document).ready(function(){
NewsRotator.init();
});
Also - I think you can improve performance slightly by caching the 'li' selector. 另外-我认为您可以通过缓存'li'选择器来稍微提高性能。
There are two ways to achieve this: 有两种方法可以实现此目的:
true
as long as the mouse is over the news item. true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.