滚动时阻止mouseenter功能

时间:2013-07-01 14:14:52

标签: jquery scroll mouseenter

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

代码:

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

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

});

1 个答案:

答案 0 :(得分:1)

您可以按如下方式实施:

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);
});

});