• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • queue()

    显示或操作匹配的元素上已经执行的函数列队。

    Contents:

    • .queue([queueName ])
      • queue([queueName ])
    • .queue([queueName ], newQueue)
      • queue([queueName ], newQueue)
      • queue([queueName ], callback(next))

    queue([queueName ])

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

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

    例子

    显示列队的长度。

    <!DOCTYPE html>
    <html>
    <head>
      <style>div { margin:3px; width:40px; height:40px;
            position:relative; left:0px; top:60px;
            background:green; display:none; }
      div.newcolor { background:blue; }
      p { color:red; }  </style>
      <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script>
    </head>
    <body>
     
      <p>The queue length is: <span></span></p>
      <div></div>
    <script>
    var div = $("div");
     
    function runIt() {
      div.show("slow");
      div.animate({left:'+=200'},2000);
      div.slideToggle(1000);
      div.slideToggle("fast");
      div.animate({left:'-=200'},1500);
      div.hide("slow");
      div.show(1200);
      div.slideUp("normal", runIt);
    }
     
    function showIt() {
      var n = div.queue("fx");
      $("span").text( n.length );
      setTimeout(showIt, 100);
    }
     
    runIt();
    showIt();
    </script>
     
    </body>
    </html>
    

    The queue length is:

    queue([queueName ], newQueue)

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

    • .queue([queueName ], newQueue)
      • queueName类型: String。一个含有队列名的字符串。默认是fx,标准的动画队列。
      • newQueue类型: Array。一个替换当前列队内容的函数数组。
    • .queue([queueName ], callback(next))
      • queueName类型: String。一个含有队列名的字符串。默认是fx,标准的动画队列。
      • callback Type: Function(Function next())要添加进队列的函数。下一个动画队列执行时会调用这个函数。

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

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

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

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

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

    $('#foo').slideUp();
    $('#foo').queue(function() {
      alert('Animation complete.');
      $(this).dequeue();
    });
    

    这是相当于:

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

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

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

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

    例子:

    Queue a custom function.

    <!DOCTYPE html>
    <html>
    <head>
      <style>
      div { margin:3px; width:40px; height:40px;
            position:absolute; left:0px; top:30px;
            background:green; display:none; }
      div.newcolor { background:blue; }
      </style>
      <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script>
    </head>
    <body>
      Click here...
      <div></div>
    <script>$(document.body).click(function () {
          $("div").show("slow");
          $("div").animate({left:'+=200'},2000);
          $("div").queue(function () {
            $(this).addClass("newcolor");
            $(this).dequeue();
          });
          $("div").animate({left:'-=200'},500);
          $("div").queue(function () {
            $(this).removeClass("newcolor");
            $(this).dequeue();
          });
          $("div").slideUp();
        });</script>
     
    </body>
    </html>
    

    Click here...

    Set a queue array to delete the queue.

    <!DOCTYPE html>
    <html>
    <head>
      <style>
      div { margin:3px; width:40px; height:40px;
            position:absolute; left:0px; top:30px;
            background:green; display:none; }
      div.newcolor { background:blue; }
      </style>
      <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script>
    </head>
    <body>
      <button id="start">Start</button>
      <button id="stop">Stop</button>
      <div></div>
    <script>$("#start").click(function () {
          $("div").show("slow");
          $("div").animate({left:'+=200'},5000);
          $("div").queue(function () {
            $(this).addClass("newcolor");
            $(this).dequeue();
          });
          $("div").animate({left:'-=200'},1500);
          $("div").queue(function () {
            $(this).removeClass("newcolor");
            $(this).dequeue();
          });
          $("div").slideUp();
        });
        $("#stop").click(function () {
          $("div").queue("fx", []);
          $("div").stop();
        });</script>
     
    </body>
    </html>
    

    下篇:dequeue()