当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。