Scroll position for the bottom of the page

I wanted to do infinite scrolling for my web application. To complete the action when reaching the bottom of the page, I tried this code:

var count = 2;
$(window).scroll(function () {
    if ($(window).scrollTop() == $(document).height() - $(window).height()) {
        alert("REached page down");
        search(false, count);
        count++;
    }
});

      

But unfortunately this happens when I scroll to the top of the page. I tried to return the scroll position as an onclick event at the bottom of the div of my page that returned:

$(window).scrollTop() as 1614
$(document).height() as 2250
$(window).height() as 2250.

      

Any other ways to achieve it?

+3


source to share


1 answer


You can check the bottom of the page with this code.

if ($(window).scrollTop() >= $(document).height() - $(window).height() - 500) {
  // fun stuff goes here
}

      



500

is the bottom offset value as you may need to run your code multiple times to prepare the content.

For example hipster.cc uses the same code for infinite scrolling.

0


source







All Articles