最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Add or Subtracting into $(window).scrollTop + $(window).height == $(document).height() - Scrolled to bottom of page

matteradmin3PV0评论

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
Add a ment  | 

1 Answer 1

Reset to default 11

Use 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) {}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far