簡體   English   中英

獲取Angular組件的滾動位置

[英]Getting the scroll position for an Angular component

如何使用fromEvent獲得組件的滾動位置?

我用它來獲取鼠標位置,如何從頂部位置滾動?

this.mouseMoveSubscription = fromEvent(this.elementRef.nativeElement, 'mousemove')
                  .subscribe((e: MouseEvent) => {...});

這不起作用:

ngOnInit() {
    this.scrollSubscription = fromEvent(this.elementRef.nativeElement, 'scroll')
                  .subscribe((e: Scroll) => {
                    console.log(e.position);
                  });

  }

也沒有觸發任何事件(來自: answer ):

@HostListener('scroll', ['$event']) // for scroll events of the current element
onScroll(event) {
  ...
}

看來您只是在錯誤的事件屬性中尋找位置。

fromEvent(this.elementRef.nativeElement,'scroll')
      .subscribe((e: Event) => console.log({
        scrollPosition: e.target['scrollTop']
        }));

看一下這個演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM