Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do something 100px before the scrollbar reaches bottom

I have the below Javascript, and the alert shows up as it is supposed to when the scrollbar hits the bottom of the page.

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

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

    alert("at bottom");

  }
}
like image 441
fbh Avatar asked Dec 27 '22 19:12

fbh


1 Answers

$(window).scroll(function(){
  if($(window).scrollTop() + 100 > $(document).height() - $(window).height() ){

    alert("at bottom");

  }
});

Using > instead of == because the scroll event fires sporadically, so you may scroll past that value a bunch of times without the event ever firing when there's a precise match.

like image 79
RwwL Avatar answered Dec 30 '22 07:12

RwwL