DEV Community

Cover image for How to get the current scroll state value of the window using JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

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

Originally posted here!

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;
Enter fullscreen mode Exit fullscreen mode
  • 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 😃.


Top comments (0)