jQuery clearQueue() 方法



jQuery 的clearQueue()方法用於移除已選元素佇列中尚未執行的所有專案。

它通常用於停止或清除尚未開始的動畫或其他排隊的函式。預設佇列名稱為"fx",所有 jQuery 動畫方法都使用此名稱。

語法

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

$(selector).clearQueue(queueName)

引數

以下是上述語法的描述:

  • queueName(可選):要清除的佇列的名稱。如果省略,則清除預設的“fx”佇列。

示例

在下面的示例中,我們使用 jQuery clearQueue() 方法來停止佇列中剩餘的函式:

<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(){
    $("#box").animate({height: 300}, 1500)
             .animate({width: 300}, 1500)
             .animate({height: 100}, 1500)
             .animate({width: 100}, 1500);
  });
  
  $("#stop").click(function(){
    $("#box").clearQueue();
    alert("Animation queue cleared!");
  });
});
</script> 
</head>
<body>

<button id="start">Start Animation</button>
<button id="stop">Stop Animation</button>
<br><br>

<div id="box" style="background:lightgreen;height:100px;width:100px;"></div>
 
</body>
</html>

執行後,當您點選“開始動畫”按鈕時,id 為“box”的 div 將執行一系列提供的動畫。

如果您在任何時候點選“停止動畫”按鈕,clearQueue() 方法將清除佇列中任何剩餘的動畫,並且一個警示框將顯示訊息:“動畫佇列已清除!”

jquery_ref_effects.htm
廣告