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:

sunset tide

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:

eyes

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:

eyes

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

  1. The preview and full-size images must have identical aspect ratios, e.g. 20x10 and 1200x600.
  2. Only vertical scrolling is checked. All images in the horizontal plane will be loaded.
  3. Progressive images dynamically added to the page using JavaScript will only be replaced when a scroll or resize event occurs.
  4. You may be able to improve actual or perceived performance using data URIs to inline images or intrinsic placeholders.
  5. Firefox can struggle when replacing large images and show a noticeable flicker.
city