Files
30-seconds-of-code/snippets/js/s/get-scroll-position.md
2023-05-07 16:07:29 +03:00

25 lines
637 B
Markdown

---
title: Scroll position
type: snippet
language: javascript
tags: [browser]
cover: tranquil-lake
dateModified: 2020-10-19T22:49:51+03:00
---
Returns the scroll position of the current page.
- Use `Window.pageXOffset` and `Window.pageYOffset` if they are defined, otherwise `Element.scrollLeft` and `Element.scrollTop`.
- Omit the single argument, `el`, to use the global `Window` object.
```js
const getScrollPosition = (el = window) => ({
x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop
});
```
```js
getScrollPosition(); // {x: 0, y: 200}
```