jQuery中的多个队列

kin*_*rey 1 javascript queue jquery animation

我在jQuery中使用多个队列时遇到问题.请考虑以下示例:

$('#example').click(function() {
  $(this).delay(1000, 'fx2').queue('fx2', function() {
    alert('here');
  });
});
Run Code Online (Sandbox Code Playgroud)

警报永远不会触发.为什么?

gna*_*arf 7

似乎在delay自定义队列上调用(或任何其他动画)时,您还需要首先使用该队列进行运动.dequeue()

.dequeue()被调用时,在队列中的下一功能从队列中取出,然后执行.该函数应该(直接或间接)导致.dequeue()被调用,以便序列可以继续.

$('#example').click(function() {
  $(this).delay(1000, 'fx2').queue('fx2', function(next) {
    alert('here');
    // start the next anim in the queue...
    next();
  }).dequeue('fx2');
});
Run Code Online (Sandbox Code Playgroud)

jsbin预览

请注意,回调queue函数接收函数作为其第一个参数.这是您在"动画"完成时要调用的函数,以便队列中的下一个项目可以执行.

jQuery代码处理函数中fx队列"自动启动" :$.fn.queue()

if ( type === "fx" && queue[0] !== "inprogress" ) {
  jQuery.dequeue( this, type );
}
Run Code Online (Sandbox Code Playgroud)

  • 更简单的方法是`function(n){alert('here'); N(); }`:) (2认同)