我正在尝试为垂直和水平滚动方向创建两个可观察的滚动事件。
我尝试使用pairwise()
andbufferCount(2,1)
运算符从水平滚动事件中过滤垂直滚动事件,但问题是获取prev.scrollTop
和的重复值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();
}
}
有任何想法吗?