To set or get the scroll position of an element, you use the properties scrollTop
and scrollLeft
of the element.
The scrollLeft
and scrollTop
properties return the number of pixels that the element’s content is scrolled from its left and top edges. The top left corner of the element is (0, 0)
. The scrollLeft
and scrollTop
are relative to that coordinate.
The following shows how to get the scroll position of the element with the class .btn
:
const el = document.querySelector('.btn');
// get scroll position in px
console.log(el.scrollLeft, el.scrollTop);
And the following shows how to set the scroll position of the .btn
element:
const el = document.querySelector('.btn');
// set scroll position in px
el.scrollLeft = 300;
el.scrollTop = 500;
Source
https://www.javascripttutorial.net/dom/css/get-and-set-scroll-position-of-an-element/