JS:
(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();
});
});
})();
它有效。当向左滑动我的 div 时,获取新的 div。但它是有序的。我想要,获取随机 div。
我该如何解决?
最佳答案
这将解决您的问题,但随机选择 div 会破坏其他 div 的动画。
(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;
}
关于javascript - 在随机 div 上使用 insertAfter 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20566056/