🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

lazy-observer

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lazy-observer

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

1.0.1
latest
Source
npm
Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

lazyObserver

lazyObserver TravisCI Node.js Bundlephobia

lazyObserver is a minimalist script to easily execute function when HTML element is intersecting. Callback can be exececuted once or every trigger.

More information about the IntersectionObserver API on MDN.

Installation

The plugin is available as the lazy-observer package name on npm and Github.

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

Environment

lazyObserver was built for Node.js >=8.11.2.

Usage

Basic usage

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

const LazyObserver = require('lazy-observer');

const lazyObserver = new LazyObserver({
    element: 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.

const LazyObserver = require('lazy-observer');

const lazyObserver = new LazyObserver({
    element: 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.

const LazyObserver = require('lazy-observer');

const lazyObserver = new LazyObserver({
    element: 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.

const LazyObserver = require('lazy-observer');

const lazyObserver = new LazyObserver({
    element: 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

HTMLElement

Tells to the function the target element.

onIntersection

function

Specifies the function to execute when the element is intersecting.

once

boolean = true

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

rootMargin

string = 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.

Keywords

intersection observer

FAQs

Package last updated on 27 Jan 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