jQuery--停止动画和判断是否处于动画状态stop()

stop()--语法结构stop([clearQueue],[gotoEnd])。

clearQueue和gotoEnd都是可选参数,为Boolean(true或者false)

1、直接使用stop(),会立即停止当前正在进行的动画,如果接下来还有动画等待继续进行,则以当前状态开始接下来的动画

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        #panel{
            position: relative;
            width: 100px;
            height: 100px;
            border:1px solid black;
            background: red;
            cursor: pointer;
            opacity: 0.5;
        }
    </style>
    <script type="text/javascript" src='jquery-3.2.1.min.js'></script>
    <script type="text/javascript">
        $(function(){
            $('#panel').hover(function(){
                $(this).stop().animate({height:'150px',width:'300px'},2000);
            },function(){
                $(this).stop().animate({height:'22px',width:'60px'},3000);
            });
        });
    </script>
</head>
<body>
    <div id="panel"></div>
</body>
</html>

如果遇到组合事件,将执行下面的动画,而非光标移出事件中的动画

$(function(){
            $('#panel').hover(function(){
                $(this).stop()
                .animate({height:'150px'},2000)//如果在此时触发了光标移出事件,将执行下面的动画,而非光标移出事件中的动画
                .animate({width:'300px'},3000);
            },function(){
                $(this).stop()
                .animate({height:'22px'},3000)
                .animate({width:'60px'},2000);
            });
});

要跳出鼠标移入触发的事件,需要给stop传递一个参数 true

$(function(){
            $('#panel').hover(function(){
                $(this).stop(true)
                .animate({height:'150px'},2000)//如果在此时触发了光标移出事件,直接跳过后面的动画队列,执行光标移出事件中的动画
                .animate({width:'300px'},3000);
            },function(){
                $(this).stop(true)
                .animate({height:'22px'},3000)
                .animate({width:'60px'},2000);
            });
});

第二个参数(gotoEnd)用于正在执行的动画直接达到结束是的状态

$(function(){
            $('#panel').hover(function(){
                $(this).stop(true,true)
                .animate({height:'150px'},2000)//如果在此时触发了光标移出事件,立刻执行完此次动画并且跳过后面的动画队列,执行光标移出事件中的动画
                .animate({width:'300px'},3000);
            },function(){
                $(this).stop(true,true)
                .animate({height:'22px'},3000)
                .animate({width:'60px'},2000);
            });
});

2、判断元素是否处于动画状态

if(!$(element).is(':animated')){  //判断元素是否正处于动画状态
                //如果当前没有进行动画,则添加新动画
}

 

转载于:https://www.cnblogs.com/halai/p/6899141.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值