1.0.3 • Published 5 years ago

@alessandrodias/lazy-loader v1.0.3

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

lazy-loader

Lazy load images using Intersection Observer.

Installation

# NPM
npm i @alessandrodias/lazy-loader

# YARN
yarn add @alessandrodias/lazy-loader

Usage:

Replace the src attribute from images with the data-lazy-src attribute

<img data-lazy-src="path/to/image.jpg">

Instantiate the class to lazy load the images

import LazyLoader from '@alessandrodias/lazy-loader/src/lazy-loader';

new LazyLoader({
  '100px 0px',
  0.5,
  yourCallbackFunction
});

Settings

optiontypedefault
rootMarginCSS margin property, e.g. "50px 0px"0
thresholdpercentage of the target's visibilitynull
callbackfunctionnull

How it works?

The Intersection Observer will observe each image with the attribute data-lazy-src and whenever they are about to appear on screen, it will set the src attribute with the value from the data-lazy-src attribute.

It also works for background images. But in this case, you must set the data-lazy-src attribute to the element that will have the background-image applied to.

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago