npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

responsively-lazy

v3.2.1

Published

Lazy load responsive images and HTML

Downloads

565

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

  • 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