在jquery上我在一个菜单链接上设置了动画背景的间隔,我的代码是
<script>
window.setInterval(function(){
$('.freesubmiteff').animate({ backgroundColor: "#46B525" }, "slow");
$('.freesubmiteff').animate({ backgroundColor: "#FFFFFF" }, "slow");
$('.freesubmiteff').animate({ backgroundColor: "#46B525" }, "slow");
$('.freesubmiteff').animate({ backgroundColor: "#FFFFFF" }, "slow");
}, 1000);
</script>
它的工作正常,但我的问题是如何在mouse over
停止动画?
答案 0 :(得分:2)
尝试
var fst = $('.freesubmiteff'); //cache selector
function ani() { //animation function
fst.animate({
backgroundColor: "#46B525"
}, "slow");
fst.animate({
backgroundColor: "#FFFFFF"
}, "slow");
fst.animate({
backgroundColor: "#46B525"
}, "slow");
fst.animate({
backgroundColor: "#FFFFFF"
}, "slow");
}
var set_int = window.setInterval(ani, 1000); //creating interval with var name set_ani
fst.hover(function () {
$(this).stop(true, true); //stop animation on hover element
clearInterval(set_int); //clear the inerval
}, function () {
ani(); //if you want animation function to start as mouse leave happens
set_int = window.setInterval(ani, 1000); //set interval again on mouse leave
});
<小时/> 参考
答案 1 :(得分:0)
将setInterval
分配给
var interval;
function fun () {
interval = window.setInterval(function(){
......
});
}
清除它
$('.freesubmiteff').on('hover', function () {
clearInterval(interval); // happens when mouseover
}, function() {
fun(); //In callback, start again the animation
});
答案 2 :(得分:0)
如何使用它?
$([selector]).mouseover(function() {
$(".freesubmiteff").finish();
});
在jquery api中查看“完成”方法。
你可以停止动画使用这种方法..
你也可以像这样使用“停止”方法..
代码1:
$(".selector").stop(true, false);
代码1看起来像当前状态下的停止动画。
代码2:
$(".selector").stop(true, true);
代码2看起来像“完成”动画..