Package Exports
- responsive-lazyload
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (responsive-lazyload) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Lazyload Images Responsively
This was inspired by https://github.com/ivopetkov/responsively-lazy/, but I found the implementation to be a little over-engineered. This solution uses similar markup, but hugely simplifies the way the actual image replacement is handled. It also adds an optional fallback for when JavaScript is disabled.
Check out the examples for more information.
Quick Start
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 { lazyLoadImages } from 'responsive-lazyload';
lazyLoadImages();
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: Manual Download
1. Download the latest release.
Download the latest release of responsive-lazyload.js from GitHub. Extract the files and place them inside your project (e.g. inside a vendor/
directory).
2. Include the script in your markup.
Just before the closing </body>
tag, add the lazyloading script:
<script src="/vendor/responsive-lazyload.js/dist/responsive-lazyload.min.js"></script>
3. Include the styles in your markup.
Include the following in the <head>
of your document:
<link rel="stylesheet"
href="/vendor/responsive-lazyload.js/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.lazyLoadImages();
</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/image@2x.jpg"
srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
data-lazyload="/images/image-300x150.jpg 300w,
/images/image-600x300.jpg 600w,
/images/image.jpg 690w,
/images/image@2x.jpg 1380w">
</div>
Markup Details
- The classes can be changed, but must be updated in the call to
lazyLoadImages()
. - The initial
srcset
is a blank GIF, which avoids an unnecessary HTTP request. - The actual
srcset
is added asdata-lazyload
.
The way lazyLoadImages()
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/image@2x.jpg"
srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
data-lazyload="/images/image-300x150.jpg 300w,
/images/image-600x300.jpg 600w,
/images/image.jpg 690w,
/images/image@2x.jpg 1380w">
<noscript>
<img alt="image description"
src="/images/image@2x.jpg"
srcset="/images/image-300x150.jpg 300w,
/images/image-600x300.jpg 600w,
/images/image.jpg 690w,
/images/image@2x.jpg 1380w">
</noscript>
</div>
JavaScript Options
To enable lazyloading, add the following to your initialization script:
import { lazyLoadImages } from './utils/lazyload-images';
lazyLoadImages({
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 git@github.com: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