4

I wanted an animation where i assign random left and top values to divs and animate them, but also wanted the animation to restart after finishing. so i fired the animate function inside itself, but after the first animation, only the last repeating div element was animating. I did like this:

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/ fiddle where i didnot use the 'for': https://jsfiddle.net/744sx34v/

2
  • for demo use <> snippet Commented Mar 3, 2017 at 6:42
  • can you pls make a clarity in fiddle link provided Commented Mar 3, 2017 at 6:49

2 Answers 2

3

Move the functionality to a seperate function and call it within the 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>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot @KAD !
You're Welcome :)
0

You don't need the for loop and need to recursively call the animate() for a specific element which also Randomly sets the top and left CSS property.

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.