jQuery stop animation

jQuery stop() method

jQuery stop() method is used to stop animations or effects before they are completed.

The stop() method works with all jQuery effect functions, including slides, fades, and custom animations.

Syntax:

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

The optional stopAll parameter specifies whether the animation queue should be cleared. The default is false, which stops only active animations and allows any queued animations to execute backwards.

The optional goToEnd parameter specifies whether to complete the current animation immediately. The default is false.

So, by default, stop() will clear the current animation specified on the selected element.

The following example demonstrates the stop() method without parameters:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
        $(document).ready(function(){
            $("#div1").click(function(){
                $("#div2").slideDown(5000);
            });
            $("#stop").click(function(){
                $("#div2").stop();
            });
        });
    </script>
    <style type="text/css">
        #div2,#div1
        {
            padding:5px;
            text-align:center;
            background-color:#e5eecc;
            border:solid 1px #c3c3c3;
        }
        #div2
        {
            padding:50px;
            display:none;
        }
    </style>
</head>
<body>
<button id="stop">停止滑动</button>
<div id="div1">点我向下滑动面板</div>
<div id="div2">我们必须接受失望,因为它是有限的,但千万不可失去希望,因为它是无穷的</div>
</body>
</html>

Run the program to try it


jQuery stop() animation (With parameters)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
        $(document).ready(function(){
            $("#start").click(function(){
                $("div").animate({left:'100px'},5000);
                $("div").animate({fontSize:'3em'},5000);
            });
            $("#stop").click(function(){
                $("div").stop();
            });
            $("#stop2").click(function(){
                $("div").stop(true);
            });
            $("#stop3").click(function(){
                $("div").stop(true,true);
            });
        });
    </script>
</head>
<body>
<button id="start">开始</button>
<button id="stop">停止</button>
<button id="stop2">停止所有</button>
<button id="stop3">停止动画,但完成动作</button>
<p>点击 "开始" 按钮开始动画。</p>
<p>点击 "停止" 按钮停止当前激活的动画,但之后我们能再动画队列中再次激活。</p>
<p>点击 "停止所有" 按钮停止当前动画,并清除动画队列,所以元素的所有动画都会停止。</p>
<p>点击 "停止动画,但完成动作" 快速完成动作,并停止它。</p>
<div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div>
</body>
</html>

Run the program and try it



Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#start").click(function(){ $("div").animate({left:'100px'},5000); $("div").animate({fontSize:'3em'},5000); }); $("#stop").click(function(){ $("div").stop(); }); $("#stop2").click(function(){ $("div").stop(true); }); $("#stop3").click(function(){ $("div").stop(true,true); }); }); </script> </head> <body> <button id="start">开始</button> <button id="stop">停止</button> <button id="stop2">停止所有</button> <button id="stop3">停止动画,但完成动作</button> <p>点击 "开始" 按钮开始动画。</p> <p>点击 "停止" 按钮停止当前激活的动画,但之后我们能再动画队列中再次激活。</p> <p>点击 "停止所有" 按钮停止当前动画,并清除动画队列,所以元素的所有动画都会停止。</p> <p>点击 "停止动画,但完成动作" 快速完成动作,并停止它。</p> <div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div> </body> </html>
submitReset Code