2016-11-04 97 views
0

我的问题:如何在jquery中正确使用mousewheel和滚动事件?

if(user reach the bottom of the page){ 
     hide element; 
    }else{ 
     show element; 
    } 

我的解决方法是使用鼠标滚轮/滚动事件处理程序知道用户在移动然后计算出如果我在页面的底部,隐藏或显示元素...

一切工作正常,但我得到这个worning的

处理“鼠标滚轮”输入事件被推迟了123毫秒,由于主线程忙。考虑将事件处理程序标记为“被动”,以使页面更具响应性。

这(我猜)是由于这样的事实,这段代码

jQuery('body').on('mousewheel', function(e){ 

运行次数太多。

那么我该如何让这段代码更高效?
有没有办法在滚动后运行事件?

我不想使用插件。

回答

1

您可以通过使用类似实现它:选中此fiddle

jQuery(window).on("scroll", function(){ 
     if($(window).scrollTop() + $(window).innerHeight() == $(document).height()) 
     { 
      //User reached end of page 
      //Hide element here 
      $("span").hide(); 
     } 
     else 
     { 
      //Show element here 
      $("span").show(); 
     } 
    });