Sometimes, we want to get and set the current web page scroll position with JavaScript.
In this article, we’ll look at how to get and set the current web page scroll position with JavaScript.
How to get and set the current web page scroll position with JavaScript?
To get and set the current web page scroll position with JavaScript, we can use the scrollTop
property.
For instance, we write
document.documentElement.scrollTop || document.body.scrollTop
to get the scroll position with either property.
And we can set the scroll position with
document.documentElement.scrollTop = document.body.scrollTop = 1000;
where 1000 is in pixels to scroll the page 1000 pixels down.
Conclusion
To get and set the current web page scroll position with JavaScript, we can use the scrollTop
property.