阻止jquery函数的双重执行?
问题描述:
我已经设置了这个功能来显示一次在导航中定位的div。阻止jquery函数的双重执行?
$(document).ready(function(){
$('.div1, .div2').hide();
$("nav a").hover(function() {
var name= this.name;
$("."+name).fadeIn().siblings().hide();
});
});
此功能,但如果我的切换非常快beteween我的导航元素,可能发生DIV1和DIV2出现在同一时间。
我该如何防止这种情况发生?
谢谢。
答
添加致电stop以在隐藏之前停止现有的动画。
$(document).ready(function(){
$('.div1, .div2').hide();
$("nav a").hover(function() {
var name= this.name;
$("."+name).fadeIn().siblings().stop(true, true).hide();
});
});
完美!这样的elegeant和简单的解决方案。很高兴知道未来的脚本。非常感谢你! – Melros
哦,我刚刚认识到,如果我切换到快速,并且div可能会在半透明状态下停止并且无法恢复,则动画会停在某一点。那是怎么回事?可能重写脚本是一个可靠的解决方案? – Melros
按照文档,您是否尝试将'true'作为第二个参数传递给'stop'? –