@ng-web-apis/intersection-observer v4.11.1
Intersection Observer API for Angular
This is a library for declarative use of Intersection Observer API with Angular.
Install
If you do not have @ng-web-apis/common:
npm i @ng-web-apis/commonNow install the package:
npm i @ng-web-apis/intersection-observerUsage
- Import
IntersectionObserverModulefor directives to work - Create IntersectionObserver with
waIntersectionObserverdirective - Observe elements with
waIntersectionObserveedirective Optional: provide root element with
waIntersectionRootdirective and usewaIntersectionThresholdandwaIntersectionRootMarginattributes to configure IntersectionObserver optionsNOTE: 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.
Examples
Observing multiple elements intersecting with viewport using single observer
<section
waIntersectionObserver
waIntersectionThreshold="0.5"
>
<div (waIntersectionObservee)="onIntersection($event)">I'm being observed</div>
<div (waIntersectionObservee)="onIntersection($event)">I'm being observed</div>
</section>Observing elements intersecting with parent element, each having different configuration therefore using individual observers:
<section waIntersectionRoot>
<div
waIntersectionObserver
waIntersectionThreshold="0.5"
(waIntersectionObservee)="onIntersection($event)"
>
I'm being observed
</div>
<div
waIntersectionObserver
waIntersectionThreshold="1,0.5,0"
(waIntersectionObservee)="onIntersection($event)"
>
I'm being observed
</div>
</section>Services
Alternatively you can use Observable-based services:
IntersectionObserveeServicecan be used to observe elements underwaIntersectionObserverdirective in the DI treeIntersectionObserverServicecan be used to observe single element independently. Provide tokens manually to configure it:
@Component({
selector: 'my-component',
providers: [
IntersectionObserverService,
{
provide: INTERSECTION_THRESHOLD,
useValue: 0.5,
},
{
provide: INTERSECTION_ROOT_MARGIN,
useValue: '10px',
},
],
})
export class Example {
constructor(@Inject(IntersectionObserverService) entries$: IntersectionObserverService) {
entries$.subscribe((entries) => {
// Don't forget to unsubscribe
console.log(entries);
});
}
}In this case provide
INTERSECTION_ROOTup the DI tree if you want to observe intersection with a particular parent element
Browser support
| 15+ | 55+ | 51+ | 12.2+ |
You can use polyfill to support older browsers
Angular Universal
If you want to use this package with SSR, you need to mock IntersectionObserver class on the server. You can use our
Universal package for this, see
this example.
Demo
You can try online demo here
See also
Other Web APIs for Angular by @ng-web-apis
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago