jQuery queue() 方法



jQuery 的 queue() 方法用於顯示將在選定元素上執行的函式佇列。

佇列是一個或多個等待執行的函式。

語法

以下是 jQuery queue() 方法的語法:

$(selector).queue(queueName)

引數

以下是上述語法的描述:

  • queueName(可選):包含佇列名稱的字串。如果省略,則使用預設的“fx”佇列。

示例

在下面的示例中,我們使用 jQuery queue() 方法來獲取將在 id 為“box”的 div 元素上執行的函式佇列的長度:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#start").click(function(){
                var box = $("#box");
                box.animate({height: "200px"});
                box.animate({width: "200px"});
                box.animate({height: "50px"});
                box.animate({width: "50px"});

                $("#queueLength").text(box.queue().length);   
            });
        });
    </script>
    <style>
        #box {
            width: 50px;
            height: 50px;
            background-color: green;
        }
    </style>
</head>
<body>

<div id="container">
    <button id="start">Trigger Animation</button>
    <p>Queue length: <span id="queueLength">0</span></p>
    <div id="box"></div>
</div>

</body>
</html>

執行上述程式後,點選按鈕(“觸發動畫”)以獲取佇列長度。

jquery_ref_effects.htm
廣告