简体   繁体   English

无限的jQuery插件不起作用

[英]Infinite jquery plugin doesn't work

So I found a snippet of code on the web which does what I want, but the problem is that is not infinite, so I want when it hits the last element to start over from the first one. 因此,我在网上找到了满足我要求的代码片段,但问题是那不是无限的,所以我希望当它到达最后一个元素时从第一个元素开始重新开始。

ORIGINAL SCRIPT 原始文字

jQuery.fn.fadeInSequence = function(fadeInTime, timeBetween)
{
    //Default Values
    timeBetween = typeof(timeBetween) == 'undefined' ? 0 : timeBetween;
     fadeInTime = typeof(fadeInTime) == 'undefined' ? 500 : fadeInTime;

    //The amount of remaining time until the animation is complete.
    //Initially set to the value of the entire animation duration.
    var remainingTime = jQuery(this).size() * (fadeInTime+timeBetween);

    var i=0; //Counter
    return jQuery(this).each(function()
    {
        //Wait until previous element has finished fading and timeBetween has elapsed
        jQuery(this).delay(i++*(fadeInTime+timeBetween));

        //Decrement remainingTime
        remainingTime -= (fadeInTime+timeBetween);

        if(jQuery(this).css('display') == 'none')
        {
            jQuery(this).fadeIn(fadeInTime);
        }
        else //If hidden by other means such as opacity: 0
        {
            jQuery(this).animate({'opacity' : 1}, fadeInTime);
        }

        //Delay until the animation is over to fill up the queue.
        jQuery(this).delay(remainingTime+timeBetween);

    }); 

};

})(jQuery);

Here is what I tried but doesn't work: 这是我尝试过但不起作用的方法:

jQuery.fn.fadeInSequence = function(fadeInTime, timeBetween)
{
    //Default Values
    timeBetween = typeof(timeBetween) == 'undefined' ? 0 : timeBetween;
     fadeInTime = typeof(fadeInTime) == 'undefined' ? 500 : fadeInTime;

    //The amount of remaining time until the animation is complete.
    //Initially set to the value of the entire animation duration.
    var remainingTime = jQuery(this).size() * (fadeInTime+timeBetween);

    var i=0; //Counter
    return jQuery(this).each(function()
    {
            if(jQuery(this).is(':last-child')){
            //Wait until previous element has finished fading and timeBetween has elapsed
            jQuery(this).parent().find('.slide').eq(0).delay(i++*(fadeInTime+timeBetween));

            //Decrement remainingTime
            remainingTime -= (fadeInTime+timeBetween);

            if(jQuery(this).parent().find('.slide').eq(0).css('display') == 'none')
            {
                jQuery(this).parent().find('.slide').eq(0).fadeIn(fadeInTime);
            }
            else //If hidden by other means such as opacity: 0
            {
                jQuery(this).parent().find('.slide').eq(0).animate({'opacity' : 1}, fadeInTime);
            }

            //Delay until the animation is over to fill up the queue.
            jQuery(this).parent().find('.slide').eq(0).delay(remainingTime+timeBetween);
                }else{
            //Wait until previous element has finished fading and timeBetween has elapsed
            jQuery(this).delay(i++*(fadeInTime+timeBetween));

            //Decrement remainingTime
            remainingTime -= (fadeInTime+timeBetween);

            if(jQuery(this).css('display') == 'none')
            {
                jQuery(this).fadeIn(fadeInTime);
            }
            else //If hidden by other means such as opacity: 0
            {
                jQuery(this).animate({'opacity' : 1}, fadeInTime);
            }

            //Delay until the animation is over to fill up the queue.
            jQuery(this).delay(remainingTime+timeBetween);
                }
    }); 

//LE // LE

(function(jQuery) {
jQuery.fn.fadeInSequence = function(fadeInTime, timeBetween)
{
    //Default Values
    timeBetween = typeof(timeBetween) == 'undefined' ? 0 : timeBetween;
     fadeInTime = typeof(fadeInTime) == 'undefined' ? 500 : fadeInTime;

    //The amount of remaining time until the animation is complete.
    //Initially set to the value of the entire animation duration.
    var remainingTime = jQuery(this).size() * (fadeInTime+timeBetween);

    var i=0; //Counter

    var counter = 0;
        var listSize = $(this).size();
while(true)
{
    $elem = $(this).get(counter);


        //Wait until previous element has finished fading and timeBetween has elapsed
        jQuery(this).delay(i++*(fadeInTime+timeBetween));

        //Decrement remainingTime
        remainingTime -= (fadeInTime+timeBetween);

        if(jQuery(this).css('display') == 'none')
        {
            jQuery(this).fadeIn(fadeInTime);
        }
        else //If hidden by other means such as opacity: 0
        {
            jQuery(this).animate({'opacity' : 1}, fadeInTime);
        }

        //Delay until the animation is over to fill up the queue.
        jQuery(this).delay(remainingTime+timeBetween);

    counter++;
    if(counter >= listSize)
    {
        counter = 0;
    }
}
 };
})(jQuery);

instead of .each, use a while loop, and a $(this).get(x) call. 代替.each,使用while循环和$(this).get(x)调用。 At the end of the loop, throw in an x++ and then set x back to 0 if it goes beyond the size of the list. 在循环结束时,抛出x ++,如果x超出列表的大小,则将其设置回0。

var counter = 0;
var listSize = $(this).size();
while(true)
{
    $elem = $(this).get(counter);
    //stuff goes here
    counter++;
    if(counter >= listSize)
    {
        counter = 0;
    }
}

edit: unfortunately, some (all?) browsers frown on infinite loops. 编辑:不幸的是,一些(全部?)浏览器皱成无限循环。 As an alternate technique, try including the jquery timers plugin. 作为一种替代技术,请尝试包括jquery timers插件。 It's essentially designed to handle repeated animations of this sort, and shouldn't scream and die in quite the same way. 它本质上是为处理此类重复动画而设计的,并且不应以完全相同的方式尖叫和死亡。 Then fit your "once through" animation function as the animation to be looped. 然后适合您的“一次通过”动画功能作为要循环播放的动画。

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

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