user516883
user516883

Reputation: 9378

call function before scrolling to the bottom of page

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

Answers (3)

Sunil Dodiya
Sunil Dodiya

Reputation: 2615

$(window).scroll(function () {
   if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
      alert('end of page');
   }
});

check this example

-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

Teneff
Teneff

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

Alok Swain
Alok Swain

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

Related Questions