下面我为一个悬停元素写了一个函数。我希望仅当鼠标停留在该部分2秒或更长时间时才执行此功能。当它们变得更少或根本不存在时,这是不可能的。
下面是我写的代码:
$(function(){
$('section.zespol_list ul li a').hover(function(){
$(this).next().fadeIn(1000);
},
function(){
$(this).next().fadeOut(1000);
});
});
发布于 2012-03-10 03:46:14
您可以使用setTimeout
来延迟执行,并且在此时间内,如果触发了mouseout,则使用clearTimeout
清除计时器。
$(function(){
var timeOutId;
$('section.zespol_list ul li a').hover(function(){
timeOutId= setTimeout(function(){
$(this).next().fadeIn(1000)
), 2000);
},
function(){
clearTimeout(timeOutId);
$(this).next().fadeOut(1000);
});
});
发布于 2012-03-10 03:46:53
使用setTimeout()
并使用clearTimeout()
清除它
var timer;
$(function(){
$('section.zespol_list ul li a').hover(function(){
timer = setTimeout(function() {
$(this).next().fadeIn(1000);
}, 2000);
},
function(){
clearTimeout(timer);
$(this).next().fadeOut(1000);
});
});
发布于 2012-03-10 03:47:28
实际上,有一个名为HoverIntent的插件。我没有亲自使用过,但看起来它可以解决你的问题
https://stackoverflow.com/questions/9640019
复制相似问题