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


JQuery stop()用法及代碼示例


stop()方法是jQuery中的內置方法,用於停止選定元素當前正在運行的動畫。

用法:

$(selector).stop(stopAll, goToEnd);

參數:此方法接受上述和以下所述的兩個參數:


  • stopAll:它是可選參數,此參數的值為boolean。此參數用於指定是否也停止排隊的動畫。此參數的默認值為false。
  • goToEnd:它是可選參數,此參數的值為boolean。此參數用於指定是否立即完成所有動畫。此參數的默認值為false。

返回值:此方法返回應用了stop方法的所選元素。

以下示例說明了jQuery中的stop()方法:

示例1:本示例不包含任何參數。

<!DOCTYPE html> 
<html> 
    <head> 
        <title>The stop Method</title> 
        <script src= 
        "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
        </script> 
          
        <!-- jQurey code to show the working of this method -->
        <script> 
            $(document).ready(function() { 
                $("#gfg_start").click(function() { 
                    $("div").animate({ 
                        height: 300 
                    }, 1000); 
                    $("div").animate({ 
                        width: 300 
                    }, 1000); 
                }); 
                $("#gfg_stop").click(function() { 
                    $("div").stop(); 
                }); 
            }); 
        </script> 
        <style> 
            div { 
                background: green; 
                height: 60px; 
                width: 60px; 
            } 
            button { 
                margin-bottom:30px; 
            } 
        </style> 
    </head> 
    <body> 
        <!-- click on this button and animation will start -->
        <button id="gfg_start">Start</button> 
        <!-- click on this button and animation will stop -->
        <button id="gfg_stop">Stop</button> 
        <div></div> 
    </body> 
</html>

輸出:

示例2:此示例包含參數。

<!DOCTYPE html> 
<html> 
    <head> 
        <title> The stop Method</title> 
        <script src= 
        "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
        </script> 
          
        <script> 
            $(document).ready(function() { 
                var div = $("div"); 
                $("#start").click(function() { 
                    div.animate({ 
                        height: 280 
                    }, "slow"); 
                    div.animate({ 
                        width: 280 
                    }, "slow"); 
                    div.animate({ 
                        height: 120 
                    }, "slow"); 
                    div.animate({ 
                        width: 120 
                    }, "slow"); 
                }); 
                $("#stop").click(function() { 
                    div.stop(true, true); 
                }); 
            }); 
        </script> 
        <style> 
            div { 
                background: green; 
                height: 100px; 
                width: 100px; 
            } 
            button { 
                margin-bottom:30px; 
            } 
        </style> 
    </head> 
    <body> 
        <!-- click on this button and animation will start -->
        <button id="start">Start </button> 
        <!-- click on this button and animation will stop -->
        <button id="stop">Stop </button> 
        <div></div> 
    </body> 
</html>

輸出:



相關用法


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