Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

Detect scroll event on element.nativeElement angular

I am using angular 8.2 and want to detect scroll event on elementRef.nativeElement. I have tried various ways:

  1. renderer2
  2. addEventListener
  3. rxJs

however, No luck.

Yes, definitely I can track event using document and window. but I want to detect on specific element.

I am using directive for the same.

I have seen that this requirement is working with rxJS 5.x and angular 4.x | 5.x. but not with angular 8.x to latest.

working example with angular 4.x and rxJs 5.x https://ashwin-sureshkumar.github.io/angular-infinite-scroller/

non working stackBlitz with latest angular

question from:https://stackoverflow.com/questions/65949987/detect-scroll-event-on-element-nativeelement-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The actual scroll code is fine. The reason it's not working in the Angular 8 version is that you're scrolling on the page, not on the div. The difference between the two is that the working example has a fixed height with overflow: scroll whereas the non-working example has the div taking up its entire height.

To get it to work, you can simply add the following css to the .scrolling-guy div:

height: 500px;
overflow-y: scroll;

Working example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...