Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect that the client is scrolled to the top or bottom of a webpage?

Tags:

javascript

I'm looking for a cross-browser method of detecting that a client web browser is scrolled all the way to the bottom (or top) of the screen.

Really, the top is fairly easy, as
scrY = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop
is zero if you're at the top. The problem is that scrY seems to return the top of the scroll bar, and not the bottom, so instead of getting something equivalent to the height of the document (in pixels) I what is presumably the height of the document less the size of the scroll bar.

Is there an easy, cross-browser way to find out if the user has scrolled down to the bottom of the document/window? Most specifically, I understand general scroll bar manipulation (setting it, moving it, etc.) but how can I get the delta of the bottom of the scrollbar's position relative to the bottom of the window/document.

like image 337
Erik Avatar asked Sep 30 '08 02:09

Erik


People also ask

How to detect if the user has scrolled to the top?

Here’s a simple way to detect if the user has scrolled to the Top or Bottom of the page using jQuery $ (window).scrollTop () – returns the current vertical position of the scroll bar.

How to detect when user scrolls to the bottom of a Div?

How to detect when user scrolls to the bottom of a div ? The task is to detect the bottom of the <div> element when user scrolls to the bottom using JQuery. Here are few methods discussed. This method adds one or more event handlers for the selected elements and child elements. Hey geek!

How to trigger an event when the user scrolled to bottom?

Sometimes you want to trigger an event, when the user has scrolled to the bottom of a page or a specific section. Scroll to bottom detection (works in all modern browsers, IE9 and up) window.onscroll = function() { if (window. innerHeight + window. pageYOffset >= document. body. offsetHeight) { alert("At the bottom!")

How to check whether the user is near to the bottom?

For checking whether the user is near to the bottom, run the following: The scroll () method either triggers the scroll event or attaches a function to invoke when a scroll event occurs. The scroll event is sent to the element when the user scrolls to different places in the element.


1 Answers

http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html

http://www.sitepoint.com/article/preserve-page-scroll-position/

http://codepunk.hardwar.org.uk/ajs02.htm

In order to ensure that an element is visible, you can use the .scrollIntoView method

like image 120
Cade Roux Avatar answered Oct 02 '22 10:10

Cade Roux