JQuery Detect Scroll at Bottom

后端 未结 10 849
南方客
南方客 2021-01-30 09:58

I wish to achieve content load when the user scrolls to the bottom of the page.

I am having a problem. It works fine on desktop browsers but not on mobile. I have imple

10条回答
  •  野性不改
    2021-01-30 10:08

    An improved jQuery version of the code

    var scrollRefresh = {
        pastTop: false,
        pastBottom: false,
        previous: 0,
        bottom: function(callback) {
          var pBottom = $(window).height() + $(window).scrollTop() >= $(document).height();
          if(!this.pastBottom && pBottom) {
            callback($(window).height() + $(window).scrollTop());
            this.pastBottom = true;
          } else {
            if(!pBottom) this.pastBottom = false;
          }
          this.previous = $(window).scrollTop();
        },
        top: function(callback) {
          var pTop = $(window).scrollTop() < this.scrollPrevious && $(window).scrollTop <= 0;
          if(!this.pastTop && pTop) {
            callback($(window).scrollTop());
            this.pastTop = true;
          } else {
            if(!pTop) this.pastTop = false;
          }
          this.previous = $(window).scrollTop();
        }
      }
    
      $(window).scroll(function() {
        scrollRefresh.top(function(pos) {
          console.log("Loading top. " + pos);
          alert("scrolled to top"); //You should delete this line
        });
        scrollRefresh.bottom(function(pos) {
          console.log("Loading bottom. " + pos);
          alert("scrolled to bottom"); //You should delete this line
        });
      });
    

提交回复
热议问题