I'm trying to create two scroll event observable for both scroll direction vertical and horizontal.
I tried using pairwise()
and bufferCount(2,1)
operators to filter vertical scroll event from the horizontal one, but the problem is with getting duplicate values for prev.scrollTop
and curr.scrollTop
import { Component, ViewChild, AfterViewInit, ElementRef } from '@angular/core';
import { fromEvent } from 'rxjs';
import { pairwise, tap, filter } from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
@ViewChild('scrollable', {static: false}) scrollable: ElementRef;
ngAfterViewInit() {
fromEvent(this.scrollable.nativeElement, 'scroll').pipe(
pairwise(),
tap(([prev, curr]) => console.log(prev.target.scrollTop, curr.target.scrollTop)),
filter(([prev, curr]) => prev.target.scrollTop !== curr.target.scrollTop),
tap((e) => console.log(e)) // <= Never reached
).subscribe();
}
}
Any ideas?