【发布时间】:2013-02-08 12:44:15
【问题描述】:
我可以设法以不同的速度对多个元素进行无限动画,但我使用了几个setInterval 函数。我的目标是用一个函数来做到这一点,当我用$.fn 尝试这个时,它们都以相同的速度制作动画。我哪里做错了?
Here is not accurate jsFiddle 和 here is targeted sample。
jQuery:
var eleHeight = $('.drop_leds').height();
var windowH = $(window).height();
var count = 0;
var counter;
var limit = windowH + eleHeight;
$.fn.goDown = function(x) {
var _this = this;
return counter = window.setInterval(function() {
if( count >= 0 && count < limit ) {
count += x;
_this.css({'top':count +'px'});
}
else if( count >= limit ) {
count=0; _this.css({'top':'-'+ eleHeight +'px'});
}
},1);
};
$('#l_0,#l_6').goDown(1);
$('#l_1,#l_4').goDown(3);
$('#l_2,#l_7').goDown(4);
$('#l_3,#l_5').goDown(2);
html/css:
<div id="l_0" class="drop_leds"></div>
<div id="l_1" class="drop_leds"></div>
<div id="l_2" class="drop_leds"></div>
<div id="l_3" class="drop_leds"></div>
<div id="l_4" class="drop_leds"></div>
<div id="l_5" class="drop_leds"></div>
<div id="l_6" class="drop_leds"></div>
<div id="l_7" class="drop_leds"></div>
.drop_leds {position:absolute; width:10px; height:60px; background:black; top:0;}
#l_0 { left:40px; }#l_1 { left:70px; }#l_2 { left:110px; }#l_3 { left:140px; }
#l_4 { left:180px; }#l_5 { left:210px; }#l_6 { left:220px; }#l_7 { left:240px; }
【问题讨论】:
-
aaah.. 让它停下来.. 当它在旁边运行时,我无法专注于你的代码......(开玩笑)
标签: javascript jquery animation setinterval infinite