我正在尝试创建两个滚动事件,无论是垂直滚动方向还是水平滚动方向都可以观察到。
我尝试使用pairwise()和bufferCount(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();
}
}有什么想法吗?
发布于 2019-06-11 09:08:03
这是因为您配对了nativeElement,它总是引用同一个对象。所以基本上你必须pluck你想要的原始值。
fromEvent(this.scrollable.nativeElement, 'scroll').pipe(
pluck('target','scrollTop'),
pairwise(),
tap(([prev, curr]) => console.log(prev,curr)),
filter(([prev, curr]) => prev!== curr),
tap((e) => console.log(e)) // <= Never reached
).subscribe();https://stackoverflow.com/questions/56534810
复制相似问题