-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscroll.ts
39 lines (34 loc) · 985 Bytes
/
scroll.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { fromEvent, merge, zip } from 'rxjs';
import {
distinctUntilChanged,
filter,
map,
pairwise,
startWith,
} from 'rxjs/operators';
export const pageScroll$ = () =>
fromEvent(window, 'scroll').pipe(
map(() => document.documentElement.scrollTop)
);
export const scrolltop$ = (el: Element) =>
merge(fromEvent(el, 'scroll')).pipe(
startWith(el.scrollTop),
map(() => el.scrollTop)
);
export const scrollHeight$ = (el: Element) =>
merge(fromEvent(el, 'scroll')).pipe(
startWith(el.scrollHeight),
map(() => el.scrollHeight)
);
export const scrollPercent$ = (el: Element) =>
zip(scrolltop$(el), scrollHeight$(el)).pipe(
filter(([top, height]) => height - el.clientHeight !== 0),
map(([top, height]) => (100 * top) / (height - el.clientHeight)),
startWith(0)
);
export const scrolldir$ = (el: Element) =>
scrolltop$(el).pipe(
pairwise(),
map(([v1, v2]) => (v1 > v2 ? 'UP' : 'DOWN')),
distinctUntilChanged()
);