【问题标题】:How to detect if the user scrolled at the end of the window? [duplicate]如何检测用户是否在窗口末尾滚动? [复制]
【发布时间】:2014-09-12 16:35:06
【问题描述】:

即使用户从结尾滚动到开头,使用以下命令也会执行操作。我只想在用户垂直向下滚动时

if( $(window).scrollTop() + $(window).height() == $(document).height() ) {

     //do stuff
}

这是我目前的代码,

   function addEvent( obj, type, fn ) {


     if ( obj.attachEvent ) {
        obj['e'+type+fn] = fn;
       obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
       obj.attachEvent( 'on'+type, obj[type+fn] );
    } else
      obj.addEventListener( type, fn, false );

   }
  addEvent(window, 'scroll', function(event) {

    if( $(window).scrollTop() + $(window).height() == $(document).height() )  {
    load();
    }

});

【问题讨论】:

标签: javascript jquery css


【解决方案1】:
window.onscroll = function(e) {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
        // do stuff
    }
};

【讨论】:

  • 不幸的是,这在 IE 上不起作用
  • 是的,好电话,我刚刚得出了同样的结论。我最初在 Chrome 中尝试过。
  • window.onscroll = function(e) { if ((window.innerHeight + document.documentElement.scrollTop) >= document.body.offsetHeight) { alert("bottom"); } };
  • 基本上用 document.documentElement.scrollTop 替换 window.ScrollY 就可以了。
  • @David Lounsbrough:是的,您的最后一个解决方案有效!但是,在处理应该交付给客户的应用程序时;我们真的不应该告诉他们必须更新浏览器才能使其正常工作。这让我想知道这是否适用于未更新的浏览器
【解决方案2】:

这行得通:

$(window).scroll(function() {   
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
       alert("bottom!");
   }
});

或者这个:

$(window).scroll(function() {   
       if($(window).scrollTop() + $(window).height() > $(document).height()) {
           alert("bottom!");
       }
    });

How to detect user reached bottom of page while scrolling

你也可能对Infinite Scroll Paging using Jquery and Ajax感兴趣

【讨论】:

  • 我试过这个,但是无论我向下滚动还是向上滚动,我的操作都是如何执行的
  • 好吧,那是我最初的尝试……帖子中提到了
  • @N.G 查看我在编辑中发布的第二个链接,我在我的应用程序中实现了它,它工作正常
  • 我也很困惑,它应该可以正常工作,但在 IE 上可以正常工作,但在 Firefox 和 chrome 上,当我向上滚动而不是向下滚动时,它可以正常工作,我可能做错了,请参阅编辑整个代码。
【解决方案3】:

试试这个

 $(window).scroll(function() {
    if ($(window).scrollTop() + $(window).height() > $(document).height()) {
        //your stuffs here
    }
});

【讨论】:

    猜你喜欢
    • 2019-04-18
    • 1970-01-01
    • 2020-07-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多