2013-07-01 58 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