加载中...

queue()


概述    .queue( [queueName ] )

返回值:Array

描述:显示在匹配的元素上的已经执行的函数列队。

  • V : 1.2.queue( [queueName ] )

    • queueName
      类型: String
      一个含有队列名的字符串。默认是 fx,标准的动画队列。

示例

实例

显示列队的长度。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>div { margin:3px; width:40px; height:40px;
  5. position:absolute; left:0px; top:60px;
  6. background:green; display:none; }
  7. div.newcolor { background:blue; }
  8. p { color:red; } </style>
  9. <script src="http://code.jquery.com/jquery-latest.js"></script>
  10. </head>
  11. <body>
  12. <p>The queue length is: <span></span></p>
  13. <div></div>
  14. <script>
  15. var div = $("div");
  16. function runIt() {
  17. div.show("slow");
  18. div.animate({left:'+=200'},2000);
  19. div.slideToggle(1000);
  20. div.slideToggle("fast");
  21. div.animate({left:'-=200'},1500);
  22. div.hide("slow");
  23. div.show(1200);
  24. div.slideUp("normal", runIt);
  25. }
  26. function showIt() {
  27. var n = div.queue("fx");
  28. $("span").text( n.length );
  29. setTimeout(showIt, 100);
  30. }
  31. runIt();
  32. showIt();
  33. </script>
  34. </body>
  35. </html>

运行一下

概述    .queue( [queueName ], newQueue )

返回值:jQuery

描述:在匹配元素上操作已经附加函数的列表。

  • V : 1.2.queue( [queueName ], newQueue )

    • queueName
      类型: String
      一个含有队列名的字符串。默认是 fx,标准的动画队列。
    • newQueue
      类型: Array
      一个替换当前列队内容的函数数组。
  • V : 1.2.queue( [queueName ], callback( next ) )

    • queueName
      类型: String
      一个含有队列名的字符串。默认是 fx,标准的动画队列。
    • callback
      Type: Function( Function next() )
      要添加进队列的函数。下一个动画队列执行时会调用这个函数。

每个元素可以通过jQuery,包含一个或多个函数队列。在大多数应用中,只有一个列队(访问 fx)被使用。队列允许一个元素来异步的访问一连串的动作,而不终止程序执行。典型的示例就是在一个元素上调用多重动画的方法对一个元素。例如:

  1. $('#foo').slideUp().fadeIn();

当这个语句被执行,这个元素开始立即做滑动动画,但渐入动画放置在 fx 列队在,只有当滑动动画完成后才会被执行。

queue()方法允许我们直接操纵这个函数队列。用一个回调函数访问queue()特别的有用;它让我们把新函数置入到队列的末端。为jQuery集合中的每个元素执行一次回调函数。

该函数的功能类似于在动画方法中提供了回调函数,但是不要求在动画执行时指定回调函数。

  1. $('#foo').slideUp();
  2. $('#foo').queue(function() {
  3. alert('Animation complete.');
  4. $(this).dequeue();
  5. });

这是相当于:

  1. $('#foo').slideUp(function() {
  2. alert('Animation complete.');
  3. });

值得注意的是,当使用.queue()添加一个函数的时候,我们应该保证在函数最后调用了 jQuery.dequeue(),这样就能让队列中的其它函数按顺序执行。

从jQuery 1.4开始,向队列中追加函数时,可以向该函数中传入另一个函数,作为第一个参数。当调用函数时,会自动从函数队列中弹出下一个项目,保证队列中函数的继续进行。我们可以像下面这样使用:

  1. $("#test").queue(function(next) {
  2. // Do some stuff...
  3. next();
  4. });

示例

实例

Queue a custom function.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. div { margin:3px; width:40px; height:40px;
  6. position:absolute; left:0px; top:30px;
  7. background:green; display:none; }
  8. div.newcolor { background:blue; }
  9. </style>
  10. <script src="http://code.jquery.com/jquery-latest.js"></script>
  11. </head>
  12. <body>
  13. Click here...
  14. <div></div>
  15. <script>$(document.body).click(function () {
  16. $("div").show("slow");
  17. $("div").animate({left:'+=200'},2000);
  18. $("div").queue(function () {
  19. $(this).addClass("newcolor");
  20. $(this).dequeue();
  21. });
  22. $("div").animate({left:'-=200'},500);
  23. $("div").queue(function () {
  24. $(this).removeClass("newcolor");
  25. $(this).dequeue();
  26. });
  27. $("div").slideUp();
  28. });</script>
  29. </body>
  30. </html>

运行一下

实例

Set a queue array to delete the queue.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. div { margin:3px; width:40px; height:40px;
  6. position:absolute; left:0px; top:30px;
  7. background:green; display:none; }
  8. div.newcolor { background:blue; }
  9. </style>
  10. <script src="http://code.jquery.com/jquery-latest.js"></script>
  11. </head>
  12. <body>
  13. <button id="start">Start</button>
  14. <button id="stop">Stop</button>
  15. <div></div>
  16. <script>$("#start").click(function () {
  17. $("div").show("slow");
  18. $("div").animate({left:'+=200'},5000);
  19. $("div").queue(function () {
  20. $(this).addClass("newcolor");
  21. $(this).dequeue();
  22. });
  23. $("div").animate({left:'-=200'},1500);
  24. $("div").queue(function () {
  25. $(this).removeClass("newcolor");
  26. $(this).dequeue();
  27. });
  28. $("div").slideUp();
  29. });
  30. $("#stop").click(function () {
  31. $("div").queue("fx", []);
  32. $("div").stop();
  33. });</script>
  34. </body>
  35. </html>

运行一下


还没有评论.