![](/img/trans.png)
[英]How to detect scroll position in Angular Material Autocomplete component
[英]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.