當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JQuery queue()用法及代碼示例


queue()方法是jQuery中的內置方法,用於顯示要在所選元素上執行的函數隊列。在隊列中,一個或多個函數等待運行。

  • queue()方法可以與dequeue()方法一起使用。
  • 一個元素可能有幾個隊列。通常,隻有一個默認的jQuery隊列。

用法:

$(selector).queue(queue_name)

參數:此方法接受可選的單個參數queue_name。用於設置隊列名稱。


下麵的示例說明了jQuery中的queue()方法:

例:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>The queue Method</title> 
        <script src= 
        "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
        </script> 
          
        <!-- jQuery code to show the working of this method -->
        <script> 
            $(document).ready(function() { 
                $("p").click(function() { 
                    var div = $("div"); 
       
                    div.animate({ 
                        left: "+=200" 
                    }, 2000); 
                    div.animate({ 
                        height: 200 
                    }, "slow"); 
                    div.animate({ 
                        width: 150 
                    }, "slow"); 
                    div.animate({ 
                        height: 100 
                    }, "slow"); 
                    div.animate({ 
                        width: 60 
                    }, "slow"); 
                    div.animate({ 
                        left: "-=200", 
                        top: "+=100" 
                    }, 2000); 
       
                    $("span").text(div.queue().length); 
                }); 
            }); 
        </script> 
        <style> 
            div { 
                width: 50px; 
                height: 50px; 
                position: absolute; 
                left: 35px; 
                margin-top: 10px; 
                background-color: green; 
            } 
               
            body { 
                width: 500px; 
                height: 250px; 
                border: 2px solid green; 
                padding: 20px; 
            } 
        </style> 
    </head> 
       
    <body> 
        <p>The queue length is: <span></span></p> 
          
        <!-- click on above paragraph to show the 
        number of times animation method works -->
        <div></div> 
    </body> 
</html>

輸出:
在單擊段落元素之前:

單擊段落元素後:



相關用法


注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 jQuery | queue() with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。