Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the scroll position of a document?

Tags:

jquery

scroll

How to get the scroll position value of a document?

like image 814
Somebody Avatar asked Aug 04 '10 15:08

Somebody


People also ask

How do I find scroll position?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

How do I get horizontal scroll position?

You can use $('html, body'). scrollLeft() to get the horizontal scrolling position if you use jQuery.

How do I find my scroll length?

To get the height of the scroll bar the offsetHeight of div is subtracted from the clientHeight of div. OffsetHeight = Height of an element + Scrollbar Height. ClientHeight = Height of an element. Height of scrollbar = offsetHeight – clientHeight.

How do I know if my scroll is at the top?

You can check if window. scrollY (the number of pixels the window has scrolled vertically) is equal to 0 . If you want to check if the window has been scrolled to its leftermost, you can check if window. scrollX (the number of pixels the window has scrolled horizontally) is equal to 0 .


1 Answers

Here's how to get the scrollHeight of an element obtained using a jQuery selector:

$(selector)[0].scrollHeight 

If selector is the id of the element (e.g. elemId), it is guaranteed that the 0-indexed item of the array will be the element you wish to select, and scrollHeight will be correct.

like image 105
Demwis Avatar answered Sep 19 '22 12:09

Demwis