2

我正在尝试为垂直和水平滚动方向创建两个可观察的滚动事件。

我尝试使用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();

  }

}

有任何想法吗?

堆叠闪电复制

4

1 回答 1

4

那是因为您将 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();
于 2019-06-11T01:08:02.677 回答