大家好,对jQuery 浅析stop()方法用法感兴趣的小伙伴,下面一起跟随三零脚本的小编来看看jQuery 浅析stop()方法用法的例子吧。
很多时候需要停止匹配元素正在进行的动画,比如,当鼠标选入元素时显示菜单,鼠标离开时隐藏下拉菜单,如果鼠标移入移出过快的话就会导致动画效果与鼠标的动作不一致的情况,此时stop()就派上用场了。
stop([clearQueue],[gotoEnd]);
参数clearQueue和gotoEnd都是可选参数,为Boolean值(true或false)。clearQueue代表是否清空未执行完的动画队列,gotoEnd代表是否直接将正在执行的动画跳转到末状态。
<!-- html部分 -->
<div id="panel" style="width: 60px;height: 22px;background-color: #eee;border: 1px solid #abcdef;font-size: 12px;">呵呵</div>
<!-- 引入jquery库 -->
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
/* js部分 */
$("#panel").hover(function() {
$(this).stop().animate({height: "150"}, 200);
},function() {
$(this).stop().animate({height: "22"}, 300);
});
</script>
$("#panel").hover(function() {
$(this).stop()
.animate({height: "150"}, 5000) //如果在此时触发了光标的移出事件
//将执行下面的动画
.animate({borderWidth: "20px"},3000); //而非光标移出事件中的动画
},function() {
$(this).stop()
.animate({width: "300"}, 2000)
.animate({fontSize: "24px"},3000);
});
$("#panel").hover(function() {
$(this).stop(true)
.animate({height: "150"}, 5000) //如果在此时触发了光标的移出事件
//直接跳过后面的动画队列
.animate({borderWidth: "20px"},3000);
},function() {
$(this).stop(true)
.animate({width: "300"}, 2000)
.animate({fontSize: "24px"},3000);
});
$("div.content")
.animate({width: "300"}, 200)
.animate({height: "150"}, 300)
.animate({opacity: "0.2"}, 200);
无论怎么设置stop()方法,均无法再改变"width"或者"height"时,将此<div>元素的末状态变成300*150大小,并且设置透明度为0.2。
温馨提示:
jQuery中的动画有show()、hide()、fadeIn()、fadeOut()、slideDown()、slideUp()、animate()等等。stop()方法对上述的动画都适用。