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


JQuery dequeue()用法及代碼示例


dequeue()方法是一個內置方法jQuery用於從隊列中刪除下一個函數,然後執行該函數。在隊列中會有多個函數等待運行dequeue(),用於從隊列中刪除頂部函數並執行該函數。

用法:

$(selector).dequeue(name);

參數:它接受一個參數“name”,該參數指定隊列的名稱。

返回值:它返回執行給定頂部函數的選定元素。

示例 1:在下麵的代碼中,dequeue()方法也用於演示出隊方法。

HTML


<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://code.jquery.com/jquery-1.10.2.js"> 
    </script> 
    
    <style> 
        div { 
            margin: 15px 0 0 0; 
            width: 100px; 
            position: absolute; 
            height: 30px; 
            left: 10px; 
            top: 30px; 
            background-color: lightgreen; 
            text-align: center; 
            padding: 15px; 
        } 
    </style> 
</head> 
  
<body> 
    <div>GfG!</div> 
    
    <!-- Click on this button to perform animation -->
    <button>Click to start !</button> 
  
    <!-- jQuery code to demonstrate animation  
        with the help of dequeue method -->
    <script> 
        $("button").click(function () { 
            $("div") 
                .animate({ left: "+=500px" }, 1000) 
                .animate({ top: "0px" }, 1000) 
                .queue(function () { 
                    $(this).toggleClass("green").dequeue(); 
                }) 
                .animate({ left: "50px", top: "150px" }, 1000); 
        }); 
    </script> 
</body> 
  
</html>

輸出:

示例 2:在此示例中,單擊按鈕時框的大小將發生變化。

HTML


<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://code.jquery.com/jquery-1.10.2.js"> 
    </script> 
    
    <style> 
        div { 
            margin: 15px 0 0 0; 
            width: 100px; 
            position: absolute; 
            height: 30px; 
            left: 10px; 
            top: 30px; 
            background-color: lightgreen; 
            text-align: center; 
            padding: 15px; 
        } 
    </style> 
</head> 
  
<body> 
    <div id="div1">GfG!</div> 
    
    <!-- Click on this button to perform animation -->
    <button>Click to start !</button> 
  
    <!--jQuery code to demonstrate animation  
        with the help of dequeue method -->
    <script>    
        $( "button" ).click(function() {    
            var div = $("#div1");     
            div.animate({width: 400});   
            div.animate({height: 300});   
            div.queue(function(){       
                div.css("background-color", "blue");     
                div.dequeue();   
            });   
            div.animate({width: 100});   
            iv.animate({height: 100});    
        });    
    </script>   
</body> 
  
</html>

輸出:



相關用法


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