progressive-image.js demonstration
progressive-image.js
implements a lazy-loading progressive image. A very small, blurred image is replaced with the full-resolution equivalent when the element is scrolled into view.
Benefits:
- fast loading, high performance, images loaded on view
- supports any image type (JPEG photographs are most appropriate)
- supports responsive images (
srcset
andsizes
attributes) - small: 1,007 bytes of JavaScript, 463 bytes of CSS (minified)
- 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.
Basic example
The page must load the CSS and JavaScript. It can be placed anywhere on the page but, typically, the CSS is loaded in the <head>
and the JS is loaded just before the closing </body>
tag:
<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 progressive
and 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>
If image loading or JavaScript fails, a blurred version of the preview image can be clicked to view the full image:
When JavaScript runs successfully, the large image is revealed when the preview is scrolled into view. CSS3 effects are used to fade and zoom the image:
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>
Responsive image example
Responsive images of differing sizes and resolutions can be defined in the link using the data-srcset
and data-sizes
attributes which
map to the standard srcset
and 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.
Usage notes
- 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.
- Progressive images dynamically added to the page using JavaScript will only be replaced when a scroll or resize event occurs.
- You may be able to improve actual or perceived performance using data URIs to inline images or intrinsic placeholders.
- Firefox can struggle when replacing large images and show a noticeable flicker.