How to get the current scroll state value of the window using JavaScript?

Published February 2, 2021

To get the current scroll state value, you can use the pageXOffset and pageYOffset properties in the global window object in JavaScript.

// Current Scroll state value from top
const scrollValuefromTop = window.pageYOffset;

// Current Scroll state value from left
const scrollValuefromLeft = window.pageXOffset;
  • The pageYOffset will give the scroll value state which is from the top of the page.
  • And the pageXOffset will the scroll value state which is from the left of the page.

See this example live in JSBin.

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter