如何在jquery中正确使用鼠标滚轮和滚动事件?

时间:2016-11-04 15:17:13

标签: jquery scroll mousewheel

我的问题:

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

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

一切正常但我有这个想法

  

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

这(我猜)是由于这段代码

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

运行太多次了。

那么我怎样才能使这段代码更有效率呢? 滚动后有没有办法运行事件?

我不想使用插件。

1 个答案:

答案 0 :(得分: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();
        }
   });