javascript - 为什么这个动画只有在第一步之后才会出现延迟?

标签 javascript jquery jquery-animate

每个动画完成后,我将 div 移动到子列表的末尾。仅在第一个 complete 函数运行后,下一个动画运行之前才会有延迟。毕竟,没有延迟。

为什么第一个有延迟?

https://jsfiddle.net/cev2Lh9w/

HTML

<div class="outer">
<div class="test">
test
</div>
<div class="test">
test
</div>
<div class="test">
test
</div>
<div class="test">
test
</div>
</div>

Javascript

jQuery( document ).ready(
function()
{
window.doms = document.querySelectorAll(".test");
window.self = document.querySelector( ".outer");
window.currentTile = doms[ 0 ];
window.currentMove = 0;
window.numberToMove = 20;
window.tileHeight = 20;
window.currentIndex = 0;
console.log( self );
moveNext();
});

function finishMove()
    {
        //Move the top tile to the end
        self.appendChild( currentTile );

        //Adjust its top
        currentTile.style.marginTop = "0px";

        //The next tile
        var next = doms[ currentIndex ];

        //Update current move and tile
        currentMove++;
        currentTile = next;

        //Change the current index
        currentIndex = ( currentIndex === doms.length - 1 ) ? 0 : currentIndex += 1;

        //Move again
        if ( currentMove < numberToMove ) moveNext();

        //We're done
        else alert( "done" );
    }

    function moveNext()
    {
        //Animate the sliding
        jQuery( currentTile ).animate(
            {
                marginTop: "-" + tileHeight + "px"
            },
            {
                duration: 200,
                queue: false,
                complete: finishMove
            }
        );
    };

最佳答案

这似乎是 jQuery 包装器的构建。如果我将它们缓存在一个数组中,然后使用该数组的 jquery 对象,则不会有延迟。

https://jsfiddle.net/cev2Lh9w/1/

//Save them as jQuery objects
window.jDoms = [];
for ( var i = 0; i < doms.length; i++ ) jDoms.push( jQuery( doms[ i ] ) );

...

//Call animate on the pre-build jQuery object
jDoms[ currentIndex ].animate(...)

关于javascript - 为什么这个动画只有在第一步之后才会出现延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39024471/

相关文章:

javascript - 如何使用 onclick 事件在同一窗口和选项卡中打开链接?

javascript - 一键两个功能 - 计算和提交表单

javascript - 如何将div从鼠标移开

javascript - 调整背景图像以适合浏览器窗口 JavaScript/HTML/CSS/JQuery

javascript - HTML 元素 : How can I simulate pressing the 'tab' key

javascript - 禁用 JavaScript 时防止表单提交

jquery - 单击导航链接时使用 jQuery 为 div 的可见性设置动画

带有动画的 jQuery setTimeout

jquery - 绑定(bind)/取消绑定(bind)计时器在 jquery 中不起作用。会解除绑定(bind)但重新激活按钮失败

jquery - 将 css 悬停转换为 jquery 悬停