Last active
June 3, 2018 21:48
-
-
Save paynoattn/63970b8f27d4fe25cf0ceb3a8e1bea86 to your computer and use it in GitHub Desktop.
Creating an Window scroll up/down Observable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Subject } from 'rxJs/Rx'; | |
const body = document.querySelector('body'); | |
let beginningScrollPostion = body.scrollTop, | |
scrollObserver = new Subject<string>(), | |
windowEventListender = window.addEventListener('scroll', (scrollEvent) => { | |
if (body.scrollTop > beginningScrollPostion) { | |
scrollObserver.next('down'); | |
} else { | |
scrollObserver.next('up'); | |
} | |
beginningScrollPostion = body.scrollTop; | |
}); | |
scrollObserver.subscribe( | |
direction => { | |
//do logic here. | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment