Javascript 无限jquery插件不';行不通
所以我在网上找到了一段代码,它做了我想做的事情,但问题是它不是无限的,所以我想当它到达最后一个元素时,从第一个元素重新开始 原始脚本 以下是我尝试过但没有成功的方法: //乐Javascript 无限jquery插件不';行不通,javascript,jquery,html,css,Javascript,Jquery,Html,Css,所以我在网上找到了一段代码,它做了我想做的事情,但问题是它不是无限的,所以我想当它到达最后一个元素时,从第一个元素重新开始 原始脚本 以下是我尝试过但没有成功的方法: //乐 使用while循环和$(this).get(x)调用代替.each。在循环结束时,抛出一个x++命令,如果x超出列表的大小,则将其设置回0 var counter = 0; var listSize = $(this).size(); while(true) { $elem = $(this).get(counte
使用while循环和$(this).get(x)调用代替.each。在循环结束时,抛出一个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;
}
}
编辑:不幸的是,一些(所有?)浏览器不喜欢无限循环。作为一种替代技术,尝试包括jquery定时器插件。它本质上是为处理这种重复的动画而设计的,不应该以完全相同的方式尖叫和死亡。然后将你的“一次性”动画功能设置为要循环的动画。请看我的帖子我更新了它,它不工作,我必须强制关闭页面…你说的“不工作”是什么意思?您看到了什么行为?无,页面正在加载。。。。我猜while循环会杀死页面,因为Chrome会问我是否想杀死页面。事实上,你是在要求一个无限循环。不过,这显然不是你需要的,所以。。。试试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
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);
}
});
(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);
var counter = 0;
var listSize = $(this).size();
while(true)
{
$elem = $(this).get(counter);
//stuff goes here
counter++;
if(counter >= listSize)
{
counter = 0;
}
}