Do something 100px before the scrollbar reaches the bottom

I have the following Javascript, and a warning appears, as it is supposed when the scroll bar gets to the bottom of the page.

However, I would like this to happen 100 pixels before it hit the bottom. How should I do it?

$(window).scroll(function(){
  if($(window).scrollTop() == $(document).height() - $(window).height() ){

    alert("at bottom");

  }
}
+3
source share
1 answer
$(window).scroll(function(){
  if($(window).scrollTop() + 100 > $(document).height() - $(window).height() ){

    alert("at bottom");

  }
});

Use >instead ==, because the scroll event fires sporadically, so you can scroll this value several times without the event that ever fires at an exact match.

+9
source

All Articles