Add or subtract in $ (window) .scrollTop + $ (window) .height == $ (document) .height () - scrolls to the bottom of the page

I run the if function when the user gets to the bottom of the page, which works just fine, as it looks

if($(window).scrollTop() + $(window).height() == $(document).height()) {}

However, I want it to run a little before the bottom - about 300 pixels earlier.

I tried

if($(window).scrollTop() + $(window).height() + 300 == $(document).height()) {}

and

if($(window).scrollTop() + $(window).height() == $(document).height() -300) {}

and all other options to no avail.

I also tried to include variables.

var plusheight = 300;

if($(window).scrollTop() + $(window).height() + plusheight == $(document).height()) {}
if($(window).scrollTop() + $(window).height() + $plusheight == $(document).height()) {}
if($(window).scrollTop() + $(window).height() + "plusheight" == $(document).height()) {}

What am I doing wrong?

+3
source share
1 answer

Use inequality. It is possible that the user scrolltop jumps right behind the exact value with which you are comparing.

if($(window).scrollTop() + $(window).height() >= $(document).height() -300) {}
+11
source

All Articles