To get the scroll position of the document, you use the following pageXOffset
, pageYOffset
properties of the window
object or the scrollLeft
and scrollTop
properties of the documentElement
object:
let scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
scrollTop = window.pageYOffset || document.documentElement.scrollTop;
Code language: JavaScript (javascript)
The following sets the scroll position of the document in px:
document.documentElement.scrollTop = document.body.scrollTop = 1200;
document.documentElement.scrollLeft = document.body.scrollLeft = 600;
Code language: JavaScript (javascript)
Was this tutorial helpful ?