jQuery stop()用法

  近期查看前辈的代码,发现在使用animate()的时候前面需要加上stop(),来防止移进移出的闪动问题,但却不知道stop()里面参数的真正意思,今天查了下stop()中参数的意义和具体使用方法,分享给大家。

  stop(true)等价于stop(true,false): 停止被选元素的所有加入队列的动画。

  stop(true,true):停止被选元素的所有加入队列的动画,但允许完成当前动画。

  stop()等价于stop(false,false):停止被选元素当前的动画,但允许完成以后队列的所有动画。

  stop(false,true):立即结束当前的动画到最终效果,然后完成以后队列的所有动画。

 

$(selector).stop(stopAll,goToEnd)

stopAll:可选。规定是否停止被选元素的所有加入队列的动画。

goToEnd:可选。规定是否允许完成当前的动画。该参数只能在设置了 stopAll 参数时使用。

第一个参数的意思是是否清空动画序列,也就是停止的是当前元素的动画效果还是停止后面附带的所有动画效果,一般为false,跳过当前动画效果,执行下一个动画效果;

第二个参数是是否将当前动画效果执行到最后,意思就是停止当前动画的时候动画效果刚刚执行了一半,这个时候想要的是动画执行之后的效果,那么这个参数就为true。否则动画效果就会停在stop执行的时候。

 

下面是从网上找的一个小例子,足够帮助大家理解stop()的使用了。

<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8'>
        <title>stop的用法案例</title>
        <style type="text/css">
            #animater{
                width: 150px;
                background:activeborder;
                border: 1px solid black;
                /*为了移动,需设置此属性*/
                position: relative;
            }
        </style>
        <script type="text/javascript" src='/style/js/jquery-1.10.2.min.js'></script>
        <script type="text/javascript">
            $(function(){
                $("#start").click(function(){
                    $("#box").animate({height:300},"slow");
                    $("#box").animate({width:300},"slow");
                    $("#box").animate({height:100},"slow");
                    $("#box").animate({width:100},"slow");
                });
                //           点击不同的button执行不同的操作
                $('#button1').click(function(){
                    //默认参数是false,不管写一个false还是两个false还是没写false效果一样
                    $('#box').stop();
                });
                $('#button2').click(function(){
                    //第二个参数默认false
                    $('#box').stop(true);
                });
                $('#button3').click(function(){
                    $('#box').stop(false,true);
                });
                $('#button4').click(function(){
                    $('#box').stop(true,true);
                });
            })
        </script>
    </head>
    <body>
        <p><input type='button' value='开始测试' id='start'></p>
        <div id="button">
            <input type="button" id="button1" value="stop()"/>
            <input type="button" id="button2" value="stop(true)"/>
            <input type="button" id="button3" value="stop(false,true)"/>
            <input type="button" id="button4" value="stop(true,true)"/> 
        </div>
        <div id="box" style="background:#98bf21;height:100px;width:100px;position:relative">stop运动参数测试</div>
    </body>
</html>

 

——非原创,侵权删

posted @ 2016-07-28 09:48  Jaye8584  阅读(751)  评论(0编辑  收藏  举报