Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 69x 69x 2x 2x | import { ElementRef, Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class VisibilityService {
private observer?: IntersectionObserver;
private observedElements = new Map<Element, () => void>();
constructor() {
Iif (('IntersectionObserver' in window)) {
this.observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const cb = this.observedElements.get(entry.target);
if (cb) {
cb();
this.observer!.unobserve(entry.target);
this.observedElements.delete(entry.target);
}
}
}, {
rootMargin: '200px'
});
});
}
}
public notifyVisible(element: ElementRef, callback: () => void) {
if (!('IntersectionObserver' in window)) {
// Fallback for unsupported browsers
callback();
return;
}
this.observer!.observe(element.nativeElement);
this.observedElements.set(element.nativeElement, callback);
}
}
|