Get scroll position of HTMLElement / window

This commit is contained in:
Renato de Leão
2017-12-12 11:52:09 +00:00
parent 82fa5f5eac
commit f275982100
2 changed files with 29 additions and 0 deletions

View File

@ -22,6 +22,7 @@
* [Fibonacci array generator](#fibonacci-array-generator) * [Fibonacci array generator](#fibonacci-array-generator)
* [Filter out non uniqe values in an array](#filter-out-non-uniqe-values-in-an-array) * [Filter out non uniqe values in an array](#filter-out-non-uniqe-values-in-an-array)
* [Flatten array](#flatten-array) * [Flatten array](#flatten-array)
* [Get scroll position](#get-scroll-position)
* [Greatest common divisor (GCD)](#greatest-common-divisor-gcd) * [Greatest common divisor (GCD)](#greatest-common-divisor-gcd)
* [Head of list](#head-of-list) * [Head of list](#head-of-list)
* [Initial of list](#initial-of-list) * [Initial of list](#initial-of-list)
@ -187,6 +188,20 @@ var flatten = arr =>
arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []); arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []);
``` ```
## Get Scroll Position
Get the current distance scrolled by `window` or `HTMLElement` as an {x,y} object
```js
const getScrollPos = (scroller = window) => {
let x = (scroller.pageXOffset !== undefined) ? scroller.pageXOffset : scroller.scrollLeft;
let y = (scroller.pageYOffset !== undefined) ? scroller.pageYOffset : scroller.scrollTop;
return {x, y}
}
// getScrollPos() -> {x: number, y: number}
```
### Greatest common divisor (GCD) ### Greatest common divisor (GCD)
Use recursion. Use recursion.

View File

@ -0,0 +1,14 @@
## Get Scroll Position
Get the current distance scrolled by `window` or `HTMLElement` as an {x,y} object
```js
const getScrollPos = (scroller = window) => {
let x = (scroller.pageXOffset !== undefined) ? scroller.pageXOffset : scroller.scrollLeft;
let y = (scroller.pageYOffset !== undefined) ? scroller.pageYOffset : scroller.scrollTop;
return {x, y}
}
// getScrollPos() -> {x: number, y: number}
```