I need an animation in which I assign random left and top values ββto divs and animate them, but also want the animation to restart after completion. so I dismissed the animation function inside myself, but after the first animation, only the last repeating div element was animated. I liked it:
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/
where I did not use 'for': https://jsfiddle.net/744sx34v/
source
share