Skip to content

Instantly share code, notes, and snippets.

@paynoattn
Last active June 3, 2018 21:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paynoattn/63970b8f27d4fe25cf0ceb3a8e1bea86 to your computer and use it in GitHub Desktop.
Save paynoattn/63970b8f27d4fe25cf0ceb3a8e1bea86 to your computer and use it in GitHub Desktop.
Creating an Window scroll up/down Observable
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