jquery - 从 jQuery 队列出队不起作用

标签 jquery jquery-animate

我预计第三个动画将会出列,但事实并非如此,我不知道为什么。这是 demo .

$('#di').animate({left:300},3000,function(){//animation callback
    $('#hello').html('1st is done');
}).animate({left:0},3000,function(){//animation callback
    $('#hello').html('2nd is done');
}).queue(function(){//queue
    $(this).animate({left:300},3000,
    function(){//animation callback
        $('#hello').html('the inside queue is done');
        $(this).dequeue();
    })
}).animate({left:0},3000,function(){//animation callback
    $('#hello').html('the last queue is done');
});

最佳答案

如果您使用传递给回调的 next 参数,则处理队列是最简单的,但在您的情况下,我认为问题是您正在尝试手动对一个队列进行排队和出队动画函数本身就扰乱了队列。如果您只使用一个不会像这样扰乱队列的普通函数,它应该可以正常工作,并且您可以使用 dequeue() 或者我更喜欢使用 next():

$('#di').animate({left:300},3000,function(){//animation callback
    $('#hello').html('1st is done');
}).animate({left:0},3000,function(){//animation callback
    $('#hello').html('2nd is done');
}).queue(function(next){//queue
    $('#hello').html('the inside queue is done');
    next();
}).animate({left:0},3000,function(){//animation callback
    $('#hello').html('the last queue is done');
});

但是,在这种情况下没有真正的理由使用 .queue.dequeue,因为所有动画都会自动进入队列,因此您可以这样做:

$('#di').animate({left:300},3000,function(){//animation callback
    $('#hello').html('1st is done');
}).animate({left:0},3000,function(){//animation callback
    $('#hello').html('2nd is done');
}).animate({left:300},3000, function(){//animation callback
        $('#hello').html('the inside queue is done');
}).animate({left:0},3000,function(){//animation callback
    $('#hello').html('the last queue is done');
});

关于jquery - 从 jQuery 队列出队不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9201000/

相关文章:

javascript - 悬停时jQuery显示框并在鼠标离开时隐藏

javascript - 如何在 webpack.config.js 中包含全日历语言环境

javascript - 如何使 jQuery scroll() 函数触发第二个函数?

jquery - 如何在两个类之间使用 jquery Animate() 和 .slideToggle()

JQuery.Color 解决方法

javascript - 重复 jquery 单击激活的动画功能

javascript - 如何使用 jquery + js 填充百分比

javascript - 使用 jquery 从一列拖放到另一列

javascript - 删除第一个选择选项

jQuery - 向注入(inject)的内容添加时间戳