如何检测用户是否在窗口末尾滚动?

时间:2014-09-12 16:35:06

标签: javascript jquery css

使用以下命令将执行操作,即使用户从结尾到开始也是如此。我希望当用户垂直向下滚动时

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

});

3 个答案:

答案 0 :(得分:0)

window.onscroll = function(e) {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
        // do stuff
    }
};

答案 1 :(得分:0)

这有效:

$(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

感兴趣

答案 2 :(得分:0)

试试这个

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