14

I use this to detect scroll to the bottom of page. But how do I detect a distance from the bottom of the page?

if($(window).scrollTop() + $(window).height() == $(document).height() ) {
 .. my ajax here
}

I mean i want the function to execute when he is 100px or 200px from the bottom, and not in the very bottom of page. How would I change this?

Also: is it possible, to instead catch if the scroll is at the last of a specific element (say my big CONTAINER) which shows the loaded content.

Thanks in advance

Ahmed Fouad
  • 2,963
  • 10
  • 30
  • 54
  • 1
    ["Infinite scroll"](http://stackoverflow.com/questions/5059526/infinite-scroll-jquery-plugin) is a feature that has been thoroughly addressed elsewhere. – Blazemonger Oct 24 '12 at 21:25

2 Answers2

20
var nearToBottom = 100;

if ($(window).scrollTop() + $(window).height() > 
    $(document).height() - nearToBottom) { 
 .. my ajax here 
} 

or to scroll to the bottom of .CONTAINER

if ($(window).scrollTop() + $(window).height() >= 
    $('.CONTAINER').offset().top + $('.CONTAINER').height() ) { 
 .. my ajax here 
} 
Doug
  • 1,018
  • 2
  • 9
  • 16
5

This will help you.

<script>
 $(window).scroll(function () {
    if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
        alert("End Of The Page");
    }
 });
</script>

Go through this link for whole article and details how it works.

load more content when browser scroll to end of page in jquery

Rohit
  • 1,653
  • 1
  • 13
  • 5