Vue.js Image Lazy Load With vue-lazyload

Dec 10, 2019

I am using vue-lazyload

Install

npm i vue-lazyload

Setup at index.js

import VueLazyload from 'vue-lazyload'Vue.use(VueLazyload)

Usage

  • Setup placeholder image via :src to avoid the plugin from hardcoding css width & height (which might not work with responsive image)
  • Consider set loading image of the same size, to avoid image layout/size changes which might causes flickering or layout redraw. Sadly there is no way to disable loading at the moment.
<template>  <div>    <div>      <template v-for="(image, index) in images" >        <template v-if="index >= 4">          <img v-lazy="{src: image, loading: loading, error: error}" :src="placeholder" :key="image" />        </template>        <template v-else>          <img :src="image" :key="image" />        </template>      </template>    </div>  </div></template><script>export default {  data() {    return {      images: [        'https://picsum.photos/id/1/150/150',        'https://picsum.photos/id/2/150/150',        'https://picsum.photos/id/3/150/150',        'https://picsum.photos/id/4/150/150',        'https://picsum.photos/id/5/150/150',        'https://picsum.photos/id/6/150/150'      ],      placeholder: 'https://placehold.co/150x150',      loading: 'https://placehold.co/150x150?text=loading',      error: 'https://placehold.co/150x150?text=error',    }  }}</script>

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.