A library for declarative use of Intersection Observer API with Angular
npm install @ng-web-apis/intersection-observer


This is a library for declarative use of
Intersection Observer API with Angular.
If you do not have @ng-web-apis/common:
``bash`
npm i @ng-web-apis/common
Now install the package:
`bash`
npm i @ng-web-apis/intersection-observer
1. Import WaIntersectionObserver for directives to workwaIntersectionObserver
2. Create IntersectionObserver with
directivewaIntersectionObservee
3. Observe elements with directivewaIntersectionRoot
4. _Optional:_ provide root element with directive and use waIntersectionThreshold andwaIntersectionRootMargin
attributes to configure
IntersectionObserver options
> NOTE: Keep in mind these are used one time in constructor so you cannot use binding, only strings. Pass comma
> separated numbers to set an array of thresholds.
Observing multiple elements intersecting with viewport using single observer
`html`
waIntersectionObserver
waIntersectionThreshold="0.5"
>
I'm being observed
I'm being observed
Observing elements intersecting with parent element, each having different configuration therefore using individual
observers:
`html
waIntersectionObserver
waIntersectionThreshold="0.5"
(waIntersectionObservee)="onIntersection($event)"
>
I'm being observed
Services
Alternatively you can use
Observable-based services:1.
IntersectionObserveeService can be used to observe elements under waIntersectionObserver directive in the DI tree2.
IntersectionObserverService can be used to observe single element independently. Provide tokens manually to
configure it:`ts
@Component({
selector: 'my-component',
providers: [
IntersectionObserverService,
{
provide: WA_INTERSECTION_THRESHOLD,
useValue: 0.5,
},
{
provide: WA_INTERSECTION_ROOT_MARGIN,
useValue: '10px',
},
],
})
export class Example {
protected readonly sub = inject(IntersectionObserverService)
.pipe(takeUntilDestroyed())
.subscribe((entries) => {
console.log(entries);
});
}
`> In this case provide
WA_INTERSECTION_ROOT` up the DI tree if you want to observe intersection with a particular|
|
|
|
|
| :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| 15+ | 55+ | 51+ | 12.2+ |
> You can use polyfill to support older browsers
You can try online demo here
Other Web APIs for Angular by
@ng-web-apis