Reputation: 9378
I know how to call a function when scrolling hits the bottom of the page.
$(window).scrollTop() == $(document).height() - $(window).height()
But I would like to do it a little bit before it hits the bottom. How would I accomplish this?
Upvotes: 2
Views: 4302
Reputation: 2615
$(window).scroll(function () {
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
alert('end of page');
}
});
-10
indicates how far away from end of page user must be before function executes. This gives you the flexibility to adjust the behavior as needed
Upvotes: 7
Reputation: 32158
Just add "a little bit before" to the equation and check if you have already passed it for example:
var a_little_bit_before = 400;
if ( $(window).scrollTop() > $(document).height() - $(window).height() - a_little_bit_before ) {
alert("TA DA!");
}
Upvotes: 0
Reputation: 6519
You are almost there.. To check whether user is near the bottom you could decide on an offset after which you decide that user is near the bottom and is about to reach it.
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - offset) {
callFunction();
}
});
Upvotes: 0