Volver al ranking

ivopetkov/responsively-lazy

JavaScriptivopetkov.com/responsively-lazy/

Lazy load responsive images and HTML

lazy-loadingwebpcsshtmljavascriptlazyloadinglazylazy-loadlazyload-imageslazyload
Crecimiento de estrellas
Estrellas
1.1k
Forks
44
Crecimiento semanal
Issues
3
5001k
feb 2023mar 2024may 2025jul 2026
Artefactosnpmnpm install responsively-lazy
README

Responsively Lazy

Probably the best lazy loading implementation available.

  • Perfect for responsive images
  • Truly lazy (absolutely no unnecessary requests)
  • SEO friendly (valid HTML)
  • Supports WebP and AVIF
  • Customizable load threshold
  • Lazy load background images
  • Lazy load HTML

You can find a demo at ivopetkov.github.io/responsively-lazy-v3/ and learn how the magic works at ivopetkov.com/responsively-lazy/

Download and install

Download the minified JS file or install via npm.

npm install responsively-lazy

The library does not have any dependencies, and it's just 2.5kb when gzipped and minified.

Usage

  • You can add the file through the CDN link in your project
  <script async src="https://cdn.jsdelivr.net/npm/responsively-lazy@3.2.1/responsively-lazy.min.js"></script>
  • Include the JS file in the head of the document
<script async src="responsively-lazy.min.js"></script>
  • Add the following code for each image
<img
    src="images/2500.jpg"
    style="width:100%;aspect-ratio:400/274;"
    srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
    data-responsively-lazy="images/400.jpg 400w, images/600.jpg 600w, images/800.jpg 800w, images/1000.jpg 1000w"
/>

Values to customize:

aspect-ratio

The aspect ratio of the image (width/height) to reserve space. Skip if you don't know it.

src

The default (largest) image size. Will be used by very old browsers, search engines, social networks, etc.

data-responsively-lazy (the star of the show)

Must contain a list of image versions separated by commas. Each version must contain a path to the image and its size. Optionaly you can specify the file type (webp or avif). The first supported format for the selected width is used.

data-responsively-lazy="images/400.avif 400w avif, images/400.webp 400w webp, images/400.jpg 400w, ..."

The paths may be encoded if there are special characters like commas, spaces, etc. Example: https%3A%2F%2Fexample.com%2Fimage.jpg

data-responsively-lazy-threshold

Specify how close to the viewport an image should be to start loading.

data-responsively-lazy-threshold="500px" // can use percents too

data-responsively-lazy-type=html

Lazy load HTML. Can contain JavaScript.

<div
    data-responsively-lazy-type="html"
    data-responsively-lazy="hello world"
></div>

data-responsively-lazy-type=background

Lazy load image as a background image.

<div
    data-responsively-lazy-type="background"
    data-responsively-lazy="images/400.jpg 400w, images/600.jpg 600w, images/800.jpg 800w, images/1000.jpg 1000w"
></div>

data-on-responsively-lazy-load

Run JavaScript code when the image is loaded.

data-on-responsively-lazy-load="..."

A new concept

Responsively Lazy is very different from the other lazy loading libraries. Others make you break your HTML by removing the src attribute, or make you put some tiny version there, or make you use <noscript> to make your images appear in search engines (like Google Images). The following code has worked for ages:

<img src="image.jpg" />

Let's not break it when we can enhance it.

<img src="image.jpg" data-responsively-lazy="image-200.jpg 200w, image-400.jpg 400w" srcset="..." />

Browser support

The lazy loading works in browsers supporting the srcset attribute and JavaScript. As of December 2023 that's 97.6%. Unsupported browsers, search engines and social networks will load the image in the src attribute. Make sure it's high resolution.

DOM changes

The library will listen for DOM changes and you can also request loading by calling responsivelyLazy.run().

License

Free to use under the MIT license.

Got questions?

You can find me at @IvoPetkovCom and ivopetkov.com

Repositorios relacionados
verlok/vanilla-lazyload

LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.

JavaScriptnpmMIT Licenselazyload-imageslazy-loading
andreaverlicchi.eu/vanilla-lazyload/
7.9k673
ApoorvSaxena/lozad.js

🔥 Highly performant, light ~1kb and configurable lazy loader in pure JS with no dependencies for responsive images, iframes and more

JavaScriptnpmMIT Licenseperformancejavascript
apoorv.pro/lozad.js/demo/
7.5k436
thebuilder/react-intersection-observer

React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport.

TypeScriptnpmMIT Licenseintersectionobserver-apireact
react-intersection-observer.vercel.app
5.5k189
Splidejs/splide

Splide is a lightweight, flexible and accessible slider/carousel written in TypeScript. No dependencies, no Lighthouse errors.

TypeScriptnpmMIT Licenseslidercarousel
splidejs.com
5.4k446
Ocramius/ProxyManager

🎩✨🌈 OOP Proxy wrappers/utilities - generates and manages proxies of your objects

PHPPackagistMIT Licenseoopproxy
5k190
tomastrajan/angular-ngrx-material-starter

Angular, NgRx, Angular CLI & Angular Material Starter Project

TypeScriptnpmMIT Licenseangularngrx
tomastrajan.github.io/angular-ngrx-material-starter
2.9k902
symfony/var-exporter

Provides tools to export, instantiate, hydrate, clone and lazy-load PHP objects

PHPPackagistMIT Licensephpsymfony
symfony.com/var-exporter
2.1k6
michalsnik/vue-content-placeholders

Composable components for rendering fake (progressive) content like facebook in vue

JavaScriptnpmMIT Licenselazy-loadingprogressive-rendering
1.6k86
Aljullu/react-lazy-load-image-component

React Component to lazy load images and components using a HOC to track window scroll position.

JavaScriptnpmMIT Licensereactreactjs
npmjs.com/package/react-lazy-load-image-component
1.6k112
wellyshen/react-cool-inview

😎 🖥️ React hook to monitor an element enters or leaves the viewport (or another element).

TypeScriptnpmMIT Licensereacthook
react-cool-inview.netlify.app
1.5k39
malchata/yall.js

A fast, flexible, and small SEO-friendly lazy loader.

JavaScriptnpmMIT Licenselazy-loadingwebperf
1.4k136
hoaphantn7604/react-native-element-dropdown

A react-native dropdown component easy to customize for both iOS and Android.

TypeScriptnpmMIT Licensereact-nativedropdown
1.3k211