progressive-image.js implements a lazy-loading progressive image similar to those seen on Facebook and Medium. A very small, blurred image is replaced with the full-resolution equivalent when the element is scrolled into view.
Please use the code as you wish - tweet me @craigbuckler if you find it useful. Download from GitHub…
- saves unnecessary bandwidth
- fast loading, high performance, images loaded on view
- supports any image type (JPEG photographs are most appropriate)
- supports responsive images (
- any CSS reveal effect can be applied
- no external dependencies - works with any framework
- works in all modern browsers (IE10+)
- progressively-enhanced to work in older browsers
- easy to use
The preview image can be very small - perhaps 20px in width and saved with high JPEG compression. This typically results in an image less than 500 bytes in size. It be added to the page directly or inlined as a data URI using base-64 encoding.
The large image can be any size but should match the preview image's aspect ratio. For example, 20x15 can be scaled to 200x150, 400x300 or 1600x1200.
<head> and the JS is loaded just before the closing
<link rel="stylesheet" href="css/progressive-image.min.css"> <script src="js/progressive-image.min.js"></script>
The simplest progressive image is a link to the full-size graphic around the preview. Note the
replace classes on the link and the
preview class on the image.
<a href="full.jpg" class="progressive replace"> <img src="tiny.jpg" class="preview" alt="image" /> </a>
After replacement, the link click is disabled and resulting HTML will be:
<a href="full.jpg" class="progressive"> <img src="full.jpg" alt="image" /> </a>
Retain the link
If you require a responsive image to remain a real link, use the
href for the address then add a
data-href attribute with the large image URL:
<a href="http://site.com/" data-href="full.jpg" class="progressive replace"> <img src="tiny.jpg" class="preview" alt="image" /> </a>
Alternative container elements
If necessary, any HTML element can be used with a
data-href attribute rather than a link, e.g.
<figure data-href="full.jpg" class="progressive replace"> <img src="tiny.jpg" class="preview" alt="image" /> </figure>
Responsive images of differing sizes and resolutions can be defined in the container link/element using the
data-sizes attributes which map to the standard
sizes attributes, e.g.
<a href="small.jpg" data-srcset="small.jpg 800w, large.jpg 1200w" data-sizes="100vw" class="progressive replace"> <img src="preview.jpg" class="preview" alt="image" /> </a>
(carriage returns added to aid legibility)
On replacement, the image code is transformed to:
<img src="small.jpg" srcset="small.jpg 800w, large.jpg 1200w" sizes="100vw" alt="image" />
Modern browsers will load large.jpg on screens of 800px width or greater.
- Works in all browsers from IE10 and above. IE10/11 will not blur the preview image. Older browsers fallback to click-to-view.
- The preview and full-size images must have identical aspect ratios, e.g. 20x10 and 1200x600.
- Only vertical scrolling is checked. All images in the horizontal plane will be loaded.
- You may improve actual or perceived performance using data URIs to inline images or intrinsic placeholders.
All images in this demonstration have been sourced from unsplash.com and are credited to Alex Blăjan, Philippe Toupet, James Padolsey, Ashim D’Silva, and Levi Guzman.