Instead of calling a method on every scroll event you could create an Observable of scroll events and subscribe to it.
Using Observable
enum Direction {
Up = 'Up',
Down = 'Down'
}
ngAfterViewInit() {
const content = document.querySelector('.mat-sidenav-content');
const scroll$ = fromEvent(content, 'scroll').pipe(
throttleTime(10), // only emit every 10 ms
map(() => content.scrollTop), // get vertical scroll position
pairwise(), // look at this and the last emitted element
map(([y1, y2]): Direction => (y2 < y1 ? Direction.Up : Direction.Down)), // compare this and the last element to figure out scrolling direction
distinctUntilChanged(), // only emit when scrolling direction changed
share(), // share a single subscription to the underlying sequence in case of multiple subscribers
);
const goingUp$ = scroll$.pipe(
filter(direction => direction === Direction.Up)
);
const goingDown$ = scroll$.pipe(
filter(direction => direction === Direction.Down)
);
goingUp$.subscribe(() => console.log('scrolling up');
goingDown$.subscribe(() => console.log('scrolling down');
}
For further explanation of the code above check out:
https://netbasal.com/reactive-sticky-header-in-angular-12dbffb3f1d3