How to get the scroll position value of a document?
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.
You can use $('html, body'). scrollLeft() to get the horizontal scrolling position if you use jQuery.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With