Last element working in each cycle

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/

+4
source share
2 answers

Move the functionality to a separate function and call it in a loop:

function getRandomArbitrary(min, max) {
      return Math.random() * (max - min) + min;
    }
    
    function anim(object){
    
    	   var aaa = getRandomArbitrary(0,100);
         var yyy = getRandomArbitrary(0,100);

         object.animate({"left":yyy+"%", "top": aaa+"%"},1000,function(){
              anim(object);
         });
     }
         
     $(".dot").each(function(){
     
        // set the initial position of each div
        var abc = getRandomArbitrary(0,100);
        var xyz = getRandomArbitrary(0,100);
    	  $(this).css({"left": abc+"%", "top": xyz+"%"});
        
        // call the animate function which calls itself recursively
    	  anim($(this));
     });
.dot{width: 8px; height: 8px; border-radius: 100%; background: red; position: absolute;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
Run codeHide result
+3
source

for animate() , CSS top left.

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

$(".dot").each(function() {
  //Initially set left and top afterwards animate will take care of it
  var left = getRandomArbitrary(0, 100);
  var top = getRandomArbitrary(0, 100);
  $this = $(this);
  $this.css({
    "left": left + "%",
    "top": top + "%"
  })
  animate($this);
});

function animate($this) {
  var left = getRandomArbitrary(0, 100);
  var top = getRandomArbitrary(0, 100);
  $this.animate({
    "left": left + "%",
    "top": top + "%"
  }, 1000, function() {
    animate($this);
  });
}
.dot {
  width: 8px;
  height: 8px;
  border-radius: 100%;
  background: red;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
Hide result
0

Source: https://habr.com/ru/post/1671329/


All Articles