javascript - 在随机 div 上使用 insertAfter 选项

标签 javascript jquery css

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();
        });
    });
})();

DEMO.

它有效。当向左滑动我的 div 时,获取新的 div。但它是有序的。我想要,获取随机 div。

我该如何解决?

最佳答案

这将解决您的问题,但随机选择 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;
}

关于javascript - 在随机 div 上使用 insertAfter 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20566056/

相关文章:

javascript - 带有翻转图像的动画侧边菜单

jquery 删除选中表格行的背景色

javascript - 如何从阵列列表中添加多个音轨

javascript - += 的异步函数

javascript - 为什么我使用 fadeTo 时会产生闪烁效果? (jQuery)

jquery - jQuery 影响不同元素后禁用 Css 悬停效果

javascript - 将局部变量传递给 AngularJs 中的指令

javascript - 单击行后不刷新

javascript - 尝试切换 CSS 属性,但我的 jquery 有问题

jquery - 使用 CSS 设置 <thead> 以占用 <table> 的整个宽度