Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load more content when user scrolls "near" bottom of page?

Tags:

jquery

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

like image 907
Ahmed Fouad Avatar asked Oct 24 '12 21:10

Ahmed Fouad


Video Answer


1 Answers

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 
} 
like image 116
Doug Avatar answered Oct 19 '22 07:10

Doug