Suppose we have a html page with large height.So there will be a vertical scrollbar.
As the user scrolls down I want to know how much he has scrolled using javascript (jquery) I suppose... Is this possible?
scrollTop . It returns by how much the content within element was scrolled, not by how much element has moved off the screen...
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.
If you want to check whether the user has scrolled to the bottom of the page, you can use the scroll() jQuery event. The given code piece takes the top scroll of the window, so how much the page is scrolled down, it adds the height of the visible window and checks if it is equivalent to the height of the document.
In pure javascript you can simply do like that:
window.onscroll = function (e) {
console.log(window.scrollY); // Value of scroll Y in px
};
More infos (The Mozilla Developer Network) :
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