GithubHelp home page GithubHelp logo

yoriiis / lazy-observer Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 721 KB

Observer module with Intersection Observer to execute a function when the element is intersecting

License: MIT License

JavaScript 72.65% TypeScript 27.35%
intersection-observer intersecting observer javascript observer-mo

lazy-observer's Introduction

lazyObserver

GitHub Workflow Status (branch) Coverage Status

lazy-observer is a minimalist script to easily execute function when HTML element is intersecting. Callback can be executed once or every trigger.

More information about the IntersectionObserver API on MDN.

Installation

NPM

NPM is the recommended installation method. Install lazy-observer in your project with the following command:

npm install lazy-observer --save-dev
yarn add lazy-observer --dev

Warning validate-target@3 is ESM.

Note Minimum supported Node.js version is 16.20.0.

CDN

You can also download it and include it with a script tag. The library will be registered as the global variable window.LazyObserver.

<script src="https://cdn.jsdelivr.net/npm/lazy-observer@2" crossorigin></script>

Note You can browse the source of the NPM package at jsdelivr.com/package/npm/lazy-observer.

How it works

Basic usage

The following example display a console.log statement when the .footer HTML element is positioned at one screen height.

new LazyObserver({
  elements: document.querySelector('.footer'),
  onIntersection: () => console.log('Function is triggered')
});

lazyObserver.observe();

Once or not

The following example displays a console.log statement each time the HTML .footer element is positioned at one screen height.

new LazyObserver({
  elements: document.querySelector('.footer'),
  once: false,
  onIntersection: () => {
    console.log('Function is triggered');
  }
});

lazyObserver.observe();

Change the offset

The following example displays a console.log statement when the HTML .footer element is positioned directly at the bottom of the screen. See the rootMargin documentation.

new LazyObserver({
    elements: document.querySelector('.footer'),
    rootMargin: '0px 0px 0px 0px'
    onIntersection: () => {
        console.log('Function is triggered');
    }
});

lazyObserver.observe();

Example with dynamic import

The following example displays a console.log statement when the HTML .footer element is positioned directly at the bottom of the screen.

new LazyObserver({
    elements: document.querySelector('.footer'),
    rootMargin: '0px 0px 0px 0px'
    onIntersection: () => {
        import(/* webpackChunkName: "footer-video" */ 'footer-video.js'
        ).then(() => {
            console.log('Module footer-video is loaded');
        });
    }
});

lazyObserver.observe();

Parameters

element

Type:

type elements = HTMLElement | HTMLElement[];

Default: null

Tells to the function the target element(s).

onIntersection

Type:

type onIntersection = () => void;

Default: () => {}

Specifies the function to execute when the element is intersecting.

once

Type:

type once = boolean;

Default: true

Specifies the function is the callback is executed once or at every trigger.

rootMargin

Type:

type rootMargin = string;

Default:

`0px 0px ${window.innerHeight}px 0px`;

Specifies the function the offset for the Intersection Observer.

Licence

lazyObserver is licensed under the MIT License.

Created with โ™ฅ by @yoriiis.

lazy-observer's People

Contributors

dependabot[bot] avatar yoriiis avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.