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

responsive-lazyload

v1.2.0

Published

A tool to lazyload images in a responsive, lightweight way (with fallbacks for unsupported browsers).

Downloads

337

Readme

Lazyload Images Responsively

Build Status Code Climate Test Coverage

This package was inspired by https://github.com/ivopetkov/responsively-lazy/. It uses very similar markup, but significantly simplifies the way image replacement is handled under the hood. It also adds an optional fallback for when JavaScript is disabled.

Quick Start

Check out the examples for copy-pasteable code and more information about usage.

Option 1: Using a Build Tool

This example assumes webpack.

1. Install the module using npm.

npm install --save responsive-lazyload

2. Include the module and initialize lazyloading.

Load the module and initialize lazyloading in your app's script:

import responsiveLazyload from 'responsive-lazyload';

responsiveLazyload();

3. Include the stylesheet.

Include the following in the <head> of your document.

<link rel="stylesheet" 
      href="node_modules/responsive-lazyload/dist/responsive-lazyload.min.css">

4. Add a lazyloaded image to your markup.

Place the lazyload markup anywhere in your app's markup:

<div class="js--lazyload">
  <img alt="a lazyloaded image"
       src="http://placekitten.com/400/300"
       srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
       data-lazyload="http://placekitten.com/400/300 1x,
                      http://placekitten.com/800/600 2x">
</div>

For more information and configuration options, see the examples.

Option 2: Using unpkg

NOTE: While unpkg is a fantastic tool, adding additional HTTP requests to your project will slow it down. For that reason, this approach is not recommended.

1. Include the script in your markup.

Just before the closing </body> tag, add the lazyloading script:

<script src="https://unpkg.com/responsive-lazyload/dist/responsive-lazyload.umd.js" defer></script>

2. Include the styles in your markup.

Include the following in the <head> of your document:

<link rel="stylesheet" 
      href="https://unpkg.com/responsive-lazyload/dist/responsive-lazyload.min.css">

3. Initialize lazyloading.

The initialization function is stored inside a global object called responsiveLazyload. Initialize lazyloading by adding the following just below the script include:

<script>
  responsiveLazyload();
</script>

4. Add a lazyloaded image to your markup.

Place the lazyload markup anywhere in your app's markup:

<div class="js--lazyload">
  <img alt="a lazyloaded image"
       src="http://placekitten.com/400/300"
       srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
       data-lazyload="http://placekitten.com/400/300 1x,
                      http://placekitten.com/800/600 2x">
</div>

For more information and configuration options, see the examples.

Markup

The markup to implement this is:

<div class="js--lazyload js--lazyload--loading">
  <img alt="image description"
       src="/images/[email protected]"
       srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
       data-lazyload="/images/image-300x150.jpg 300w,
                    /images/image-600x300.jpg 600w,
                    /images/image.jpg 690w,
                    /images/[email protected] 1380w">
</div>

Markup Details

  • The classes can be changed, but must be updated in the call to responsiveLazyload().
  • The initial srcset is a blank GIF, which avoids an unnecessary HTTP request.
  • The actual srcset is added as data-lazyload.

The way responsiveLazyload() works is to check if the image is inside the viewport, and — if so — swap out the srcset for the data-lazyload. This is much simpler than duplicating browser behavior to choose the optimal image size; instead, we just give the browser a srcset and let it do its thing.

On browsers that don’t support srcset, the regular src attribute is used, so this should degrade gracefully.

Markup With Fallback for Browsers Without JavaScript Enabled

To ensure that an image is still visible, even if JavaScript is disabled, add a <noscript> block with the properly marked up image using srcset without the lazyloading solution:

<div class="js--lazyload js--lazyload--loading">
  <img alt="image description"
       src="/images/[email protected]"
       srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
       data-lazyload="/images/image-300x150.jpg 300w,
                    /images/image-600x300.jpg 600w,
                    /images/image.jpg 690w,
                    /images/[email protected] 1380w">
  <noscript>
    <img alt="image description"
         src="/images/[email protected]"
         srcset="/images/image-300x150.jpg 300w,
                 /images/image-600x300.jpg 600w,
                 /images/image.jpg 690w,
                 /images/[email protected] 1380w">
  </noscript>
</div>

JavaScript Options

To enable lazyloading, add the following to your initialization script:

import responsiveLazyload from 'responsive-lazyload';

responsiveLazyload({
    containerClass: 'js--lazyload',
    loadingClass: 'js--lazyload--loading',
    callback: () => {},
});

option | default | description ---------------- | ----------------------- | ----------------------------------- containerClass | js--lazyload | Determines which elements are targeted for lazyloading. loadingClass | js--lazyload--loading | Applied to containers before loading. This is useful for adding loading animations. callback | () => {} | Fired on each image load. Useful for adding custom functionality after an image has loaded.

Development

To run this module locally for development, follow these steps:

# Clone the repo.
git clone [email protected]:jlengstorf/responsive-lazyload.js.git

# Move into the repo.
cd responsive-lazyload.js/

# Install dependencies.
npm install

# Run the build script.
npm run build

Testing

Tests are built using Jest. Run them with:

npm test

# Or, to remove all the extra crap npm spits out and only show test output:
npm test --silent