Javascript 延迟()或带停止()的超时?
我希望Javascript 延迟()或带停止()的超时?,javascript,jquery,hover,delay,Javascript,Jquery,Hover,Delay,我希望mouseenter函数具有stop()和1秒的延迟。 因此,如果我将鼠标悬停在#download上,fadeIn将在1秒延迟后启动。如果同时将鼠标移出,fadeIn将不会启动。明白吗 我真的不知道怎么做,有什么想法吗?使用setTimeout函数 $('.file a').live('mouseenter', function() { $('#download').stop(true, true).fadeIn('fast'); }).live('mouseleave', fun
mouseenter
函数具有stop()
和1秒的延迟。
因此,如果我将鼠标悬停在#download
上,fadeIn
将在1秒延迟后启动。如果同时将鼠标移出,fadeIn
将不会启动。明白吗
我真的不知道怎么做,有什么想法吗?使用setTimeout函数
$('.file a').live('mouseenter', function() {
$('#download').stop(true, true).fadeIn('fast');
}).live('mouseleave', function() {
$('#download').stop(true, true).fadeOut('fast');
});
setTimeout将在指定的毫秒(在本例中为1000)后执行函数内的代码 您需要在这种情况下使用,因为它的工作原理(以及您无法取消它)
如果使用它,将使该元素的下一个动画出列,而不管您之前是否清除了该队列。因此,您需要一个可以取消的超时时间,如上所述,通过手动调用并存储结果,以便以后可以通过来清除它。您可以在jquery上使用它,而无需使用延迟事件
$('.file a').live('mouseenter', function() {
$.data(this, 'timer', setTimeout(function() {
$('#download').stop(true, true).fadeIn('fast');
}, 1000));
}).live('mouseleave', function() {
clearTimeout($.data(this, 'timer'));
$('#download').stop(true, true).fadeOut('fast');
});
1000是你的时间,在它之后,$(“#下载”)将淡入。我正在寻找一个类似问题的答案,我发现.animate()也可以用来处理这个问题,它遵守.stop() 它看起来像这样:
$('.file a').hover(function() {
time = setTimeout(function() {
$('#download').fadeIn();
},1000);
},function(){
clearTimeout(time);
});
您还需要存储/清除该超时,如果您快速地将鼠标悬停在该元素上,它将在200毫秒内完成fadeOut()(如果它运行的话),然后在800毫秒后有一个排队的fadeIn,即使您没有在该元素上。看看这里,看看我的意思:要进行测试,请快速悬停并离开链接。@user239831-您对此有什么悬而未决的问题吗?非常好。延迟是非常重要的。我仍然知道没有办法取消,这意味着应该非常小心地使用它。这是一个很好的解决方法。谢谢,你好。我正在为
slideDown()
和slideUp()
尝试相同的方法,但效果不好。你能告诉我我错过了什么吗?注意:我尝试在不使用hoverIntent()
函数的情况下执行此操作。从doco:astjquery1.7开始,不推荐使用.live()
方法。使用.on()
附加事件处理程序。
$('.file a').hover(function() {
time = setTimeout(function() {
$('#download').fadeIn();
},1000);
},function(){
clearTimeout(time);
});
$('.file a').live('mouseenter', function() {
$('#download')
.stop(true, true)
.animate({opacity:0}, 1000); // one second delay
.animate({opacity:1}, 'fast', 'swing');
}).live('mouseleave', function() {
$('#download')
.stop(true, true)
.animate({opacity:0}, 'slow', 'swing');
});