Basically I would like to flag a dom element with an @input value, i.e. 'aos'
<div aos>my</div>
And then for example do a console log whenever such div is within my viewport.
How can this be achieved? I assume using Angular's Scrolling Dispatcher, but how can I understand that such div is in the viewport?
LIVE DEMO
You can use an IntersectionObserver
for doing that. It's a native browser API, not an Angular one. One approach is to build a directive that, when applied to any element will tell you that element is visible/hidden. You can check its API here.
Basically what you have to do in this directive is:
ElemenRef
)IntersectionObserver
@Directive({selector: "[enterTheViewportNotifier]"})
export class EnterTheViewportNotifierDirective implements AfterViewInit, OnDestroy {
@Output() visibilityChange = new EventEmitter<'VISIBLE' | 'HIDDEN'>();
private _observer: IntersectionObserver;
constructor(@Host() private _elementRef: ElementRef) {}
ngAfterViewInit(): void {
const options = {root: null,rootMargin: "0px",threshold: 0.0};
this._observer = new IntersectionObserver(this._callback, options);
this._observer.observe(this._elementRef.nativeElement);
}
ngOnDestroy() {this._observer.disconnect();}
private _callback = (entries, observer) => {
entries.forEach(entry =>
this.visibilityChange.emit(entry.isIntersecting ? 'VISIBLE' : 'HIDDEN'));
};
}
And you can use it like this (_visibilityChangeHandler
will be called with a message everytime the below div
enters/leaves the viewport):
<div (visibilityChange)="_visibilityChangeHandler($event)"
enterTheViewportNotifier>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With