Often, you come across web pages which have lots of images, and with most of them out of the viewport, the browsers still loading them even thought they’re not visible.
There’s lazy loading, but the downside is that while the images are loading you see an unpleasant empty space. That’s where progressive image rendering comes into play.
Progressive image rendering is a technique used to improve the user experience for lazy loaded images by starting loading a very lightweight image as a placeholder while loading the real image, so that the user doesn’t see the empty space until the actual image is loaded.
There are several ways to do the placeholder. Some examples:
In this articles we’ll focus on the blur effect placeholder.
v-lazy-image
is a Vue.js component built on top of the example shown in the article Lazy Image Component using the Intersection Observer API, with some features added.
P.S.: the component is written by yours trully. 😉
First, you need to install it first in a Vue.js app:
$ npm install v-lazy-image
Then, import the component and use it by passing an src
property to it:
<template>
<v-lazy-image src="http://lorempixel.com/400/200/" />
</template>
<script>
import VLazyImage from "v-lazy-image";
export default {
components: {
VLazyImage
}
};
</script>
Just like that, the image will be lazy loaded as soon as it becomes visible. You can pass any attributes like alt
and they’ll be added to the <img>
tag.
v-lazy-image
allows to set a placeholder by using the src-placeholder
property:
<v-lazy-image
src="https://cdn-images-1.medium.com/max/1600/1*xjGrvQSXvj72W4zD6IWzfg.jpeg"
src-placeholder="https://cdn-images-1.medium.com/max/80/1*xjGrvQSXvj72W4zD6IWzfg.jpeg"
/>
Additionally, it exposes the class v-lazy-image-loaded
in order to apply any kind of CSS animation and styling. For example, you can easily apply a blur effect:
.v-lazy-image {
filter: blur(10px);
transition: filter 0.7s;
}
.v-lazy-image-loaded {
filter: blur(0);
}
In this demo you can see this one and other funny examples of CSS animations applied to v-lazy-image.
v-lazy-image
also exposes the @load
and @intersect
events in case you need more control.
For example, animating the blur filter by CSS is quite heavy, but we can build a more performant version using an SVG:
<template>
<div>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="filter hidden">
<defs>
<filter id="blur">
<feGaussianBlur in="SourceGraphic" :stdDeviation="deviation" />
</filter>
</defs>
</svg>
<v-lazy-image
:src="src"
:src-placeholder="srcPlaceholder"
@load="animate"
></v-lazy-image>
</div>
</template>
<script>
export default {
props: {
src: String,
srcPlaceholder: String,
blurLevel: {
type: Number,
default: 30
},
duration: {
type: Number,
default: 1000
}
},
data: () => ({ rate: 1 }),
computed: {
deviation() {
return this.blurLevel * this.rate;
}
},
methods: {
animate() {
const start = Date.now() + this.duration;
const step = () => {
const remaining = start - Date.now();
if (remaining < 0) {
this.rate = 0;
} else {
this.rate = remaining / this.duration;
requestAnimationFrame(step);
}
};
requestAnimationFrame(step);
}
}
};
</script>
<style scoped>
.filter {
display: none;
}
.v-lazy-image {
width: 100%;
filter: url("#blur");
}
</style>
It gets more complex indeed, but is more performant and you can wrap it in a reusable component like that one.
You can see it running in this Codesandbox.
There are multiple and creative ways to do progressive image rendering. v-lazy-image makes that easy by providing a performant and customizable lazy image component, so you don’t have to. If you want to give any feedback, that’d be much appreciated 😉.
Stay cool 🦄
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.