Easy Angular Way To Detect If Element Is In Viewport

[Solved] Easy Angular Way To Detect If Element Is In Viewport | Swift - Code Explorer | yomemimo.com
Question : Easy Angular way to detect if element is in viewport on scroll

Answered by : samer-saeid

// the element that you are observing (just add #yourElement in the template) for this query to work @ViewChild('yourElement') yourElement: ElementRef; ngAfterViewInit() { const threshold = 0.2; // how much % of the element is in view const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { // run your animation code here observer.disconnect(); // disconnect if you want to stop observing else it will rerun every time its back in view. Just make sure you disconnect in ngOnDestroy instead } }); }, { threshold } ); observer.observe(this.yourElement.nativeElement); }

Source : | Last Update : Wed, 18 May 22

Answers related to easy angular way to detect if element is in viewport on scroll

Code Explorer Popular Question For Swift