2012-02-08 22 views
2

我期待的是,第三个动画将要离队,但它不是,我不知道为什么。这是一个demo出列从jQuery的队列不起作用

$('#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'); 
}); 
+0

Omg,代码放屁!请正确缩进。 – elclanrs 2012-02-08 20:40:34

+1

请重申您的问题,因为它不清楚您问的是什么。 – jfriend00 2012-02-08 20:41:48

+0

第三个动画必须在第二个动画之后执行,但它不是 – Amged 2012-02-08 20:44:30

回答

2

你需要考虑在其中,每个功能能够添加一些队列中的命令的条款。

事情之前有机会动画,队列会这个样子......

animate // left:300 
callback 
animate // left:0 
callback 
callback via queue 
animate // left:0 
callback 

请记住,任何已经开始之前,所有发生。

问题在于您的dequeue正在回调中添加到发生在您的queue()回调中的动画中。这意味着,新的回调函数被放置在队列的末尾...

animate // run, then automatically dequeue 
callback // run, then automatically dequeue 
animate // run, then automatically dequeue 
callback // run, then automatically dequeue 
callback via queue // run, place the new animation and callback on the end + 
animate //                 | 
callback //                 | 
                      | 
animate // <---------------------------------------------------------------+ 
callback // The dequeue() happens in here 

所以你可以看到,你出列粘贴在年底,所以队列您queue()回调后卡住。

+0

你的意思是说,传递任何函数来.queue像.queue(fun(){}) 使传递函数在队列数组的最后一个索引? – Amged 2012-02-08 21:14:12

+1

@已收到:差不多。基本上你需要记住,在任何动画开始之前,所有的同步代码都会运行。所以主链有animate()。animate()。queue()。animate()'。所以这就是队列的样子,但插入回调之间。因此,当你的'queue()'回调函数最终运行时,嵌套在内部的'animate()'被添加到结尾。由于您没有在'queue()'回调中直接'出队',所以队列就停留在那里。 – 2012-02-08 21:18:47

+1

...如果直接在'queue()'callback *(而不是嵌套的'animate()'回调)中执行'dequeue()',那么您将成功出队,但嵌套'animate()仍然会持续到最后,所以它会超出预期的顺序。 – 2012-02-08 21:19:44

3

与队列处理是最容易,如果你使用的是传递给回调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'); 
}); 
+0

这不是关于动画,我只是想了解队列,并且我得到了这个问题,所以我想明白为什么发生这种情况? – Amged 2012-02-08 20:48:43

+0

'.dequeue()'很难理解(我还没有完全弄明白)。如果你在我的第一个代码示例中使用'next()'参数,它每次都有效,所以我就是这么做的。 – jfriend00 2012-02-08 20:51:31

+0

另一个问题,有什么办法可以用来执行队列数组的某个索引? – Amged 2012-02-08 20:54:44

0

简答题:将queue: false选项添加到animate调用函数内,参见this jsFiddle

朗的答案与解释:

答案通过@ jfriend00是几乎没有在这个意义上,animate呼叫在queue功能本身处理队列中。由于@Amged询问了更多细节,我认为除了提供解决方案之外,我还会填补空白。

在jQuery中每一个元素可以有一组命名队列。默认情况下,在元素上调用animate当动画继续该元素的“FX”队列。同样,调用queue没有明确的队列名称将放置功能的“FX”队列。

因此,发生的事情是animate调用被其包含的queue函数阻塞。

的解决方案是队列中的动画:

$('#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}, { 
     duration: 3000, 
     // Don't queue the animation, otherwise it gets queued on the default 
     // queue ('fx') and since we're currently running in this queue, it 
     // would block 
     queue: false, 
     complete: 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'); 
}); 

于我的回答一开始,你可以找到一个work demo here提及。