I'm running an if function when the user gets to the bottom of the page which works great as is like this
if($(window).scrollTop() + $(window).height() == $(document).height()) {}
However I want it to run slightly before the bottom - about 300px before.
I've tried
if($(window).scrollTop() + $(window).height() + 300 == $(document).height()) {}
AND
if($(window).scrollTop() + $(window).height() == $(document).height() -300) {}
and all other variations to no avail.
I've also tried putting variables in.
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?
I'm running an if function when the user gets to the bottom of the page which works great as is like this
if($(window).scrollTop() + $(window).height() == $(document).height()) {}
However I want it to run slightly before the bottom - about 300px before.
I've tried
if($(window).scrollTop() + $(window).height() + 300 == $(document).height()) {}
AND
if($(window).scrollTop() + $(window).height() == $(document).height() -300) {}
and all other variations to no avail.
I've also tried putting variables in.
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?
Share asked May 2, 2012 at 7:01 JamesJames 3,2934 gold badges43 silver badges57 bronze badges 2- 3 It's hard to get exactly 300px above the bottom... – user1150525 Commented May 2, 2012 at 7:03
-
2
It's hard to a user scrolls exactly to -300px from bottom. You should use greater then
>
or less then<
. – Emre Erkan Commented May 2, 2012 at 7:04
1 Answer
Reset to default 11Use an inequality. It's quite possible the user's scrolltop is jumping right past the exact value you're paring with.
if($(window).scrollTop() + $(window).height() >= $(document).height() -300) {}