简体   繁体   English

暂停使用jQuery进行的无限循环

[英]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: 有两种方法可以实现此目的:

  1. Clear the animation and then restart it. 清除动画,然后重新启动它。 That will also make sure the gap to the next item will always be constant. 这也将确保与下一项的差距将始终保持不变。
  2. Use a global flag to return early from the rotation function. 使用全局标志从旋转功能中早返回。 Set the flag to true as long as the mouse is over the news item. 只要鼠标悬停在新闻项上,就将标志设置为true

Take a look at this JQFAQ.com , here is the answer for pause and resume the timer. 看看这个JQFAQ.com ,这是暂停和恢复计时器的答案。 here there are more FAQs available, i hope this will help you to get more ideas 这里有更多常见问题解答,我希望这会帮助您获得更多想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM