React Lazily IMG
React Lazily IMG is a React Wrapper Component to lazily load images. The goal is to use the convenient and known standard HTML tags and just have them lazily loaded.
- Picture tag and IMG srcset support
- Webp detection
- HTML && CSS image support
- First render when image is downloaded
- Customize percentage of visibility
- Custom Callback when image is rendered
npm install react-lazily-img --save
How does it work ?
React Lazily IMG is powered by the performant Intersection Observer API. No need of annoying unperformant scroll event listeners and elem.getBoundingClientRect() to check if an element is in the viewport.
The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.
import React from 'react';// import React Lazily IMGimport Lazy from 'react-lazily-img';// import imagesimport Image1 from './images/1.jpg';import Image1WebP from './images/1.webp';import Image2 from './images/2.jpg';import Image2WebP from './images/2.webp';import Image3 from './images/3.jpg';import Image3WebP from './images/3.webp';import Placeholder from './images/placeholder.jpg';import PlaceholderWebp from './images/placeholder.webp';// You dont need to set webp to true when working with a picture tag as its has a webp detection on its own.const options =waitComplete: falsewebp: false;const App props =><div ="App"><Lazy ><picture><source ="image/webp" ="(min-width:600px)" = /><source ="image/webp" ="(min-width:500px)" = /><source ="(min-width:600px)" = /><source ="(min-width:500px)" = /><img = = ="butterfly" /></picture></Lazy></div>;
<Lazy ><img==="(max-width: 320px) 280px,(max-width: 480px) 440px,800px"="butterfly"==/></Lazy>
<Lazy >// data-webpsrc and the optional data-webpplaceholder are only needed// if you enable webp detection in the options// placeholder are optional. Image that is shown until the image is in the viewport<img = = = = ="butterfly" /></Lazy>
DIV tag - CSS background
<Lazy ><div = = /></Lazy>
A placeholder is an image that is shown until the image is in the viewport. data-webpsrc and the optional data-webpplaceholder are only needed if you enable webp detection in the options.
<Lazy ><img = = = = ="butterfly" /></Lazy>
Multiple images in one wrapper
You can also place multiple images in one wrapper
<Lazy ><img = ="butterfly" /><div = /><picture><source ="(min-width:600px)" = /><source ="(min-width:500px)" = /><img = ="butterfly" /></picture></Lazy>
||Wait until the image is fully downloaded before rendering. Doesn't yet support the picture tag.||bool||
||Ship a webp version if Browser supports it. No need to enable it when working with picture tag as it has its own detection (
||Hides the image until its rendered as you would otherwise see the alt tag.||bool||
||Clear the data-attributes you used to pass the image after its rendered.||bool||
||Takes a custom callback thats executed after the image is rendered.||func||
||The element that is used as the viewport to check the visiblity of a target.||elem||browser viewport|
||Similair to the CSS 'margin' property. It manipulates the elements bounding box. Same syntax as in CSS with either an absolute length or a percentage.||px || %||
||Indicate at what percentage of the target's visibility the observer's callback should be executed. i.e at an visibility above 50%:
||num (0 -> 1)||
const options =waitComplete: truewebp: truehideTillRender: trueclearAttributes: trueconsoleroot: documentrootMargin: '50px 0px'threshold: 075;<Lazy >// image</Lazy>
For the full support IntersectionObserver polyfill is used.