1

I need to check scroll event for a "show more" features.

Im using:

window.addEventListener('scroll', this.scroll, true);

and this code:

scroll = (event: any): void => {
        const number = event.srcElement.scrollTop;

        this.show_scroll_top = false;
        if (event.target.scrollTop >= 500) {
            this.show_scroll_top = true;
        }

        if (event.target.offsetHeight + event.target.scrollTop >= event.target.scrollHeight) {
              this.showMorFunctionFoo();
        }

    };

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?

1 Answer 1

3

One solution might be to use hostListener from Angular (https://angular.io/api/core/HostListener) in your page component

@HostListener('scroll', ['$event']) 
onScroll($event:Event):void {
   ... your logic here
};

or you could use something like that

scroll = (event: any): void => {
    if (event.target.getAttribute('id') === YOU_PAGE_HTML_ID) {
       ... your logic here
    } 
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It works with this workaround: if (event.target.tagName === "MAT-SIDENAV-CONTENT") ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.