我想要一个动画,其中我将随机左值和顶值赋值给div并为其设置动画,但也希望动画在完成后重新启动。所以我发射了自己的动画函数,但在第一个动画之后,只有最后一个重复的div元素是动画。我不喜欢这样的:在每个循环中工作的最后一个元素
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
$(".dot").each(function(){
var abc = getRandomArbitrary(0,100);
var xyz = getRandomArbitrary(0,100);
var aaa = getRandomArbitrary(0,100);
var yyy = getRandomArbitrary(0,100);
$(this).css({"left": abc+"%", "top": xyz+"%"})
$thiss = $(this);
for(var i=0; i< $(".dot").length;i++){
function anim(){
$thiss.animate({"left":yyy+"%", "top": aaa+"%"},1000,function(){
anim();
});
console.log(i)
}
}
anim();
});
HTML:
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
CSS:
.dot{width: 8px; height: 8px; border-radius: 100%; background: red; position: absolute;}
的jsfiddle:https://jsfiddle.net/qfcmfzw7/ 小提琴这里我因此未使用 '为':https://jsfiddle.net/744sx34v/
用于演示使用'<>'片断 – guradio
你请可以做一个清晰的小提琴链接提供 –