I need to check scroll event for a “show more” features.
Im using:
JavaScript
x
2
1
window.addEventListener('scroll', this.scroll, true);
2
and this code:
JavaScript
1
14
14
1
scroll = (event: any): void => {
2
const number = event.srcElement.scrollTop;
3
4
this.show_scroll_top = false;
5
if (event.target.scrollTop >= 500) {
6
this.show_scroll_top = true;
7
}
8
9
if (event.target.offsetHeight + event.target.scrollTop >= event.target.scrollHeight) {
10
this.showMorFunctionFoo();
11
}
12
13
};
14
Unfortunally the scroll listen to every scroll in page, for example a scroll inside a DropDown etc…
How can I listen ONLY the PAGE scroll ignoring everyelse?
Advertisement
Answer
One solution might be to use hostListener from Angular (https://angular.io/api/core/HostListener) in your page component
JavaScript
1
5
1
@HostListener('scroll', ['$event'])
2
onScroll($event:Event):void {
3
your logic here
4
};
5
or you could use something like that
JavaScript
1
6
1
scroll = (event: any): void => {
2
if (event.target.getAttribute('id') === YOU_PAGE_HTML_ID) {
3
your logic here
4
}
5
};
6