Skip to content

Instantly share code, notes, and snippets.

@fireflysemantics
Created December 14, 2019 22:34
Show Gist options
  • Save fireflysemantics/e8b3414e6ab20eff78c7ad7ebe77e6d5 to your computer and use it in GitHub Desktop.
Save fireflysemantics/e8b3414e6ab20eff78c7ad7ebe77e6d5 to your computer and use it in GitHub Desktop.
/**
* @param scrollable The element being scrolled
* @param debounceMS The number of milliseconds to debounce scroll events
* @param sp The function returning the scroll position coordinates.
* @return A boolean valued observable indicating whether the element is scrolling up or down
*/
export function scrollingUp(
scrollable: any,
debounceMS: number,
sp: scrollPosition): Observable<boolean> {
return fromEvent(scrollable, 'scroll').pipe(
debounceTime(debounceMS),
distinctUntilChanged(),
map(v => sp()),
pairwise(),
switchMap(p => {
const y1 = p[0][1]
const y2 = p[1][1]
return y1 - y2 > 0 ? of(false) : of(true)
}))
}
export type scrollPosition = ()=>[number, number]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment