2013-07-01 53 views
1

我试图阻止用户在jQuery中滚动时发生mouseenter函数,但无法弄清楚,有什么建议吗?滚动时阻止mouseenter函数

代码:

$(".li").mouseenter(function(){ 
$(this).children(".work_name").toggleClass("open", 400); 

$('.li').mouseleave(function(){ 
    $(this).children(".work_name").removeClass("open", 400); 
}); 

}); 

回答

0

如下您可以实现它:

window.isScrolling = false; 
$(window).scroll(function() { 
    window.isScrolling = true; 
    clearTimeout($.data(this, "scrollTimer")); 
    $.data(this, "scrollTimer", setTimeout(function() { 
     // If the window didn't scroll for 250ms 
     window.isScrolling = false; 
    }, 250)); 
}); 

然后改变你这样的代码:

$(".li").mouseenter(function(){ 

// Prevent executing when the user is scrolling 
if (window.isScrolling) return; 

$(this).children(".work_name").toggleClass("open", 400); 

$('.li').mouseleave(function(){ 
    $(this).children(".work_name").removeClass("open", 400); 
}); 

}); 
+0

完美的作品,非常感谢你! – RoseCoder