每个循环中的最后一个元素

时间:2017-03-03 06:39:52

标签: jquery

我想要一个动画,我将随机的左侧和顶部值分配给div并为它们设置动画,但也希望动画在完成后重新启动。所以我在其自身内部启动了animate函数,但在第一个动画之后,只有最后一个重复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/ 我没有使用'for'的小提琴:https://jsfiddle.net/744sx34v/

2 个答案:

答案 0 :(得分:3)

将功能移至单独的功能并在循环中调用它:

&#13;
&#13;
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));
     });
&#13;
.dot{width: 8px; height: 8px; border-radius: 100%; background: red; position: absolute;}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您不需要for循环,需要递归调用animate()以获取特定元素,该元素也随机设置topleft CSS属性。

&#13;
&#13;
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);
  });
}
&#13;
.dot {
  width: 8px;
  height: 8px;
  border-radius: 100%;
  background: red;
  position: absolute;
}
&#13;
<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>
&#13;
&#13;
&#13;