jQuery.queue()

Contents:

jQuery.queue( element, [ queueName ] ) 官网原E文: Array

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

  • version added: 1.3jQuery.queue( element, [ queueName ] )

    element一个用于检查附加列队的DOM元素。

    queueName一个含有队列名的字符串。默认是"Fx",标准的动画队列。

注意: 这是一个底层的方法,你应该用.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; }
  span { color:red; }  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <button id="show">Show Length of Queue</button>
  <span></span>
  <div></div>
<script>$("#show").click(function () {
      var n = jQuery.queue( $("div")[0], "fx" );
      $("span").text("Queue length is: " + n.length);
    });
    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);
    }
    runIt();</script>

</body>
</html>

Demo:

jQuery.queue( element, queueName, newQueue ) 官网原E文: jQuery

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

  • version added: 1.3jQuery.queue( element, queueName, newQueue )

    element一个已附加列队函数(数组)的DOM元素 。

    queueName一个含有队列名的字符串。默认是"Fx",标准的动画队列。

    newQueue一个替换当前函数列队内容的数组。

  • version added: 1.3jQuery.queue( element, queueName, callback() )

    element一个要附加列队函数的DOM元素。

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

    callback()添加到列队的新函数。

注意: 这是一个底层的方法,你应该用.queue()代替。

每个元素可以通过jQuery包含一个或多个函数队列。在大多数应用中,只有一个列队(访问 fx)被使用。队列允许一个元素来异步的访问一连串的动作,而不终止程序执行。

jQuery.queue()方法允许我们直接操纵这个函数队列。用一个回调函数访问jQuery.queue()特别的有用;它让我们把新函数置入到队列的末端。

值得注意的是,当使用jQuery.queue()添加一个函数的时候,我们必须保证jQuery.dequeue()让下一个函数执行后被呼叫 。

举例:

例子: 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="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  Click here...
  <div></div>
<script>
   $(document.body).click(function () {
      $("div").show("slow");
      $("div").animate({left:'+=200'},2000);
      jQuery.queue( $("div")[0], "fx", function () {
        $(this).addClass("newcolor");
        jQuery.dequeue( this );
      });
      $("div").animate({left:'-=200'},500);
      jQuery.queue( $("div")[0], "fx", function () {
        $(this).removeClass("newcolor");
        jQuery.dequeue( this );
      });
      $("div").slideUp();
    });</script>

</body>
</html>

Demo:

例子: 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="http://code.jquery.com/jquery-latest.min.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);
      jQuery.queue( $("div")[0], "fx", function () {
        $(this).addClass("newcolor");
        jQuery.dequeue( this );
      });
      $("div").animate({left:'-=200'},1500);
      jQuery.queue( $("div")[0], "fx", function () {
        $(this).removeClass("newcolor");
        jQuery.dequeue( this );
      });
      $("div").slideUp();
    });
    $("#stop").click(function () {
      jQuery.queue( $("div")[0], "fx", [] );
      $("div").stop();
    });
</script>

</body>
</html>

Demo:

jQuery 1.6 API 中文版Clove整理、修订