我想要一個動畫,其中我將隨機左值和頂值賦值給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
你請可以做一個清晰的小提琴鏈接提供 –