2.0.7 • Published 4 years ago

lb-lazy-images v2.0.7

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

lb-lazy-images v2

Image Lazy loading utility for Parcel-bundler.
If the IntersectionObserver API is not supported/polyfilled, all images will eagerload.
1.6KB Gzipped

Features

  • Built-in Caching system to prevent partial image rendering.
  • Use plain HTML syntax to set img data-src and data-srcset.
  • No fallback setup to worry about for old browsers, it's already taken care of.
  • Built-in Event hooks for easy animations and debugging.

Requirements

  • parcel-bundler

Usage

HTML

Important :

  • All images have to be located in src/assets.
    data-src and data-srcset paths start from src/assets excluded
  • If you have a different file structure URLs and imported filenames are still supported.
<img 
	src="placeholder.jpg" 
	data-src="folder/filename.jpg"
	data-srcset="folder/filename1.jpg 500w, folder/filename2.jpg 700w, ..."
/>

Load on scroll with the IntersectionObserver :

Basic usage :

import LazyLoader from "lb-lazy-images";

LazyLoader.loadOnScroll('.selector')

Customize the IntersectionObserver :

//defaults:
const options= {
	root: viewport,
	rootMargin: '500px 500px 500px 500px',
	threshold: 0.01
}

LazyLoader.loadOnScroll('.selector', options)

Add some callback if you wish :

LazyLoader
	.loadOnScroll('.selector')
	.onIntersection( img => do something )
	.onLoad( img => do something )
	.onError( img => do something )
	.onVisible( img => img.classList.add('fadein') ) //animate here
	.onAllSettled( images => do something )

Or load with some event :

button.addEventListener('click', ()=>{

	LazyLoader
		.loadAll('.selector')
		.onLoad( img => do something )
		.onError( img => do something )
		.onAllSettled( images => do something )

})

Generate lots of lazy loading images with JS :

Import the sources + library :

import sources from '../src/assets/*.*';
import LazyLoader from  "lb-lazy-images"

Generate a map of images appended to the DOM :

const sourceMap = Object.values(sources).map(val => (
	Object.values(val)[ 0 ]
));
const imageMap = sourceMap.map(src => {
	const img = document.createElement('IMG');
	img.dataset.src = src;
	//do more stuff
	container.appendChild(img);
	return img;
});

Setup Lazy Loading using the map :

LazyLoader
	.loadOnScroll(imageMap)
	.onVisible(img => img.classList.add('fadein'));

Loading methods :

MethodArg 1Arg 2Description
loadOnScrollString, Node, NodeList, Array, HTMLCollectionObjectLoads the images with the IntersectionObserver API. Fallsback to loadAll for older Browsers
loadAllString, Node, NodeList, Array, HTMLCollectionN/AStarts loading all the images when called.

Event hooks :

MethodloadOnScrollloadAllDescription
onIntersectionXFires when the image hits the margin of the IntersectionObserver root. Image will start loading here. Will fire just before onLoad if the intersectionObserver is not supported.
onLoadXXFires when the image loads. The image src/srcset are loaded in the DOM here.
onErrorXXFires on loading error. The image src/srcset are still loaded in the DOM here.
onVisibleXFires when the image enters the IntersectionObserver root. Will fire right after onLoad if the intersectionObserver is not supported.
onAllSettledXXFires when all images have finished loading, whether or not there was an error.

⭐ ⭐ ⭐

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.3.18

4 years ago

1.3.17

4 years ago

1.3.10

4 years ago

1.3.13

4 years ago

1.3.14

4 years ago

1.3.11

4 years ago

1.3.12

4 years ago

1.3.15

4 years ago

1.3.16

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago