Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to certain height of a page javascript

Tags:

javascript

I've a very simple one line code that checks whether user has scrolled to the bottom of the page, I want to change it a little bit & find whether user has reached the footer of the page. And height of footer is somewhat 350px.

Here's my code:

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

This works like a charm (loads more content on scroll event), but if I do like this:

if($(window).scrollTop() + $(window).height() == ($(document).height()-350))

This doesn't work. When I attempt to alert('$(document).height()-350'). It gives a perfect alert.

Can anyone say what I'm doing wrong?

like image 639
Shanil Soni Avatar asked Mar 13 '26 09:03

Shanil Soni


1 Answers

you are probably scrolling more than 1 pixel at a time and just skip the equality point. make it a >= and it should work:

if($(window).scrollTop() + $(window).height() >= ($(document).height()-350))
like image 107
Banana Avatar answered Mar 15 '26 23:03

Banana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!