事情有点像这样:
$("example").hover(function(){
("example").stop().show(/*More Code*/, callback());
}, function(){
("example").stop().hide(/*More Code*/, callback());
}
)
因此,如果鼠标在第一个动画完成之前离开该区域,它将停止所有动画,并跳过回调。有没有办法使用 stop() 并仍然执行回调函数?
最佳答案
使用 setTimeout
它定时在动画结束时触发,而不是回调。
$("example").hover(function(){
$("example").stop().show(1000);
setTimeout(function() { /* do something */ }, 1000);
}, function(){
$("example").stop().hide(1000);
setTimeout(function() { /* do something */ }, 1000);
}
);
另一种选择是使用 .stop(true,true)
而不仅仅是 .stop()
。
这会清除队列,将动画发送到末尾,并触发回调。
$("example").hover(function(){
$("example").stop(true,true).show(1000);
}, function(){
$("example").stop(true,true).hide(1000);
}
);
关于jquery - 在 jquery 中停止父函数时防止回调被停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4812615/