Forked from realtomaszkula/ng-reactive-sticky-header#is-visible-flag.ts
Last active
June 16, 2020 09:45
-
-
Save sunzxs/620fbbe59e40cf23409d1f09bcc5e8ff to your computer and use it in GitHub Desktop.
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
@Component({ | |
selector: 'app-sticky-header', | |
template: `<ng-content></ng-content>`, | |
styles: [ ... ] | |
}) | |
export class StickyHeaderComponent implements AfterViewInit { | |
private isVisible = true; | |
ngAfterViewInit() { | |
const scroll$ = fromEvent(window, 'scroll').pipe( | |
throttleTime(10), | |
map(() => window.pageYOffset), | |
pairwise(), | |
map(([y1, y2]): Direction => (y2 < y1 ? Direction.Up : Direction.Down)), | |
distinctUntilChanged(), | |
share() | |
); | |
const scrollUp$ = scroll$.pipe( | |
filter(direction => direction === Direction.Up) | |
); | |
const scrollDown = scroll$.pipe( | |
filter(direction => direction === Direction.Down) | |
); | |
scrollUp$.subscribe(() => (this.isVisible = true)); | |
scrollDown$.subscribe(() => (this.isVisible = false)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment