$('.file a').live('mouseenter', function() {
$('#download').stop(true, true).fadeIn('fast');
}).live('mouseleave', function() {
$('#download').stop(true, true).fadeOut('fast');
});
我希望
mouseenter
函数具有stop()
和1秒的延迟。因此,如果我将鼠标悬停在
#download
上,则fadeIn
应该在1秒钟的延迟后开始。如果我将鼠标移出,则fadeIn
不应该开始。救我我真的不知道该怎么做,有什么想法吗?
最佳答案
在这种情况下,您需要使用 setTimeout()
,因为 .delay()
的工作方式(而且您无法取消它)。
$('.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');
});
You can give it a try here。
如果您使用
.delay()
,它将使该元素的下一个动画出队,无论您是否早先清除了该队列。因此,您需要一个可以取消的超时,以上操作是通过手动调用 setTimeout()
并将结果存储在 $.data()
中来实现的,以便稍后可以通过 clearTimeout()
清除它。