Skip to content

Instantly share code, notes, and snippets.

@andrit
Created July 10, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrit/45e2ae6faf182487eb4e325ae7f04ef0 to your computer and use it in GitHub Desktop.
Save andrit/45e2ae6faf182487eb4e325ae7f04ef0 to your computer and use it in GitHub Desktop.

Get scroll position

Use pageXOffset and pageYOffset if they are defined, otherwise scrollLeft and scrollTop. You can omit el to use a default value of window.

const getScrollPos = (el = window) =>
  ({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
    y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop});
// getScrollPos() -> {x: 0, y: 200}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment