Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ng-web-apis/intersection-observer

Package Overview
Dependencies
Maintainers
3
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ng-web-apis/intersection-observer

A library for declarative use of Intersection Observer API with Angular

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

Intersection Observer API for Angular

Part of Web APIs for Angular

npm version npm bundle size Travis (.com) Coveralls github angular-open-source-starter

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/common

Now install the package:

npm i @ng-web-apis/intersection-observer

Usage

  1. Create IntersectionObserver with waIntersectionObserver directive

  2. Observe elements with waIntersectionObservee directive

  3. Optional: provide root element with waIntersectionRoot directive and use waIntersectionThreshold and waIntersectionRootMargin 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..

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:

  1. IntersectionObserveeService can be used to observe elements under waIntersectionObserver directive in the DI tree

  2. IntersectionObserverService can 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 MyComponent {
    constructor(
        @Inject(IntersectionObserverService) entries$: IntersectionObserverService,
    ) {
        entries$.subscribe(entries => {
            // Don't forget to unsubscribe
            console.log(entries);
        });
    }
}

In this case provide INTERSECTION_ROOT up the DI tree if you want to observe intersection with a particular parent element

Browser support

IE / EdgeFirefoxChromeSafari
15+55+51+12.2+

You can use polyfill to support older browsers

Demo

You can try online demo here

See also

Other Web APIs for Angular by @ng-web-apis

Open-source

Do you also want to open-source something, but hate the collateral work? Check out this Angular Open-source Library Starter we’ve created for our projects. It got you covered on continuous integration, pre-commit checks, linting, versioning + changelog, code coverage and all that jazz.

Keywords

FAQs

Package last updated on 26 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc