2013-12-13 23 views
1

JS:随机div的使用insertAfter选项

(function animate() { 
    $("#houses div:first").each(function(){ 
     $(this).animate({marginLeft:-$(this).outerWidth(true)},1000,'linear',function(){ 
      $(this).insertAfter("#houses div:last"); 
      $(this).css({marginLeft:0}); 
      animate(); 
     }); 
    }); 
})(); 

DEMO.

它的工作原理。当滑动离开我的div,得到新的div。但它是有序的。我想要得到随机的div。

我该如何解决?

+0

你怎么想,当他们为了滑出获得随机div的? –

+0

对不起,我不明白你... – user3099199

+0

你想滑块得到随机div或重命名div中的值? –

回答

0

这将解决您的问题,但挑选随机div将打破其他div的动画。

Working Fiddle

(function animate() { 
    $("#houses div:first").each(function(){ 
     $(this).animate({marginLeft:-$(this).outerWidth(true)},1000,'linear',function(){ 
      $(this).insertAfter("#houses div:nth-child("+getRandomInt(1,9)+")"); 
      $(this).css({marginLeft:0}); 
      animate(); 
     }); 
    }); 
})(); 

function getRandomInt (min, max) { 
return Math.floor(Math.random() * (max - min + 1)) + min; 
} 
+0

谢谢。但我只想得到最后一个孩子的随机div。 – user3099199

+0

这就是代码的作用。它不会给你带来平滑的效果,因为你正在从动画中挑选一个随机div。 –