vue3-lazyload

April 10, 2026 ยท View on GitHub



A Vue 3 lazyload plugin for images and component subtrees.

๐Ÿš€ Features

  • โšก 0 runtime dependencies: No worry about your bundle size
  • ๐Ÿฆพ Type Strong: Written in Typescript
  • ๐ŸŒŽ Vue 3 only: Built for Vue 3 applications and libraries
  • ๐ŸŒŽ Browser support: Use it through CDN
  • ๐Ÿ˜Š Support Hook: useLazyload
  • ๐Ÿงฑ Support LazyComponent: Defer mounting slot content until it enters the viewport
  • ๐Ÿ” Support modes and events: Keep content mounted once loaded, or mount/unmount with visibility events

๐Ÿ“Ž Installation

$ npm i vue3-lazyload
# or
$ yarn add vue3-lazyload
# or
$ pnpm add vue3-lazyload

๐ŸŒŽ CDN

CDN: https://unpkg.com/vue3-lazyload

<script src="https://unpkg.com/vue3-lazyload"></script>
<script>
  Vue.createApp(App).use(VueLazyLoad)
  ...
</script>

๐Ÿ‘ฝ Usage

This package supports Vue 3 only.

main.js:

import { createApp } from 'vue'
import VueLazyLoad from 'vue3-lazyload'
import App from './App.vue'

const app = createApp(App)
app.use(VueLazyLoad, {
  // options...
})
app.mount('#app')

App.vue:

<template>
  <img v-lazy="your image url" />
</template>

Which API Should I Use?

APIBest forWhat it controls
v-lazyRegular template imagesSwaps image src when the element becomes visible
useLazyloadImages managed inside component logicReuses plugin defaults and lets the component drive source updates
LazyComponentExpensive cards, charts, or nested subtreesDelays mounting a whole slot subtree

v-lazy and useLazyload are image-oriented. LazyComponent is for component-level lazy mounting.

v-lazy use object params

<template>
  <img v-lazy="{ src: 'your image url', loading: 'your loading image url', error: 'your error image url' }">
</template>

Use lifecycle

In main.js

import { createApp } from 'vue'
import VueLazyLoad from 'vue3-lazyload'
import App from './App.vue'

const app = createApp(App)
app.use(VueLazyLoad, {
  loading: '',
  error: '',
  lifecycle: {
    loading: (el) => {
      console.log('loading', el)
    },
    error: (el) => {
      console.log('error', el)
    },
    loaded: (el) => {
      console.log('loaded', el)
    }
  }
})
app.mount('#app')

or

In xxx.vue

Do not pass a whole reactive object as v-lazy="lazyOptions". Use inline object fields instead, otherwise Vue cannot track nested changes the way you expect.

<script>
import { reactive } from 'vue'
export default {
  name: 'App',
  setup() {
    const lazyOptions = reactive({
      src: 'your image url',
      lifecycle: {
        loading: (el) => {
          console.log('image loading', el)
        },
        error: (el) => {
          console.log('image error', el)
        },
        loaded: (el) => {
          console.log('image loaded', el)
        }
      }
    })
    return {
      lazyOptions,
    }
  }
}
</script>

<template>
  <img v-lazy="{ src: lazyOptions.src, lifecycle: lazyOptions.lifecycle }" width="100">
</template>

Use Hook

<script lang="ts">
import { ref } from 'vue'
import { useLazyload } from 'vue3-lazyload'

export default {
  name: 'App',
  setup() {
    const src = ref('https://picsum.photos/id/64/720/420')
    const lazyRef = useLazyload(src, {
      lifecycle: {
        loading: () => {
          console.log('loading')
        },
        error: () => {
          console.log('error')
        },
        loaded: () => {
          console.log('loaded')
        }
      }
    })

    function switchSource() {
      src.value = 'https://picsum.photos/id/65/720/420'
    }

    return {
      lazyRef,
      switchSource,
    }
  }
}
</script>

<template>
  <img ref="lazyRef" class="image" width="320">
  <button @click="switchSource">
    Switch source
  </button>
</template>

If you install the plugin with app.use(VueLazyLoad, options), useLazyload() will reuse the injected lazyload instance by default. If you pass local options to the hook, they are merged on top of the plugin defaults without mutating the global instance.

Use LazyComponent

Use LazyComponent when the expensive part is not an image but a whole component subtree.

<template>
  <LazyComponent
    :delay="220"
    mode="once"
  >
    <template #placeholder>
      <div class="card-skeleton">
        Loading card...
      </div>
    </template>

    <ExpensiveChart />
  </LazyComponent>
</template>

LazyComponent supports two modes:

  • mode="once": mount the slot the first time it becomes visible, then keep it mounted
  • mode="visible": mount on enter, unmount on leave, and restore the placeholder

Example with visibility events:

<script setup lang="ts">
import { ref } from 'vue'

const visible = ref(false)
const mounts = ref(0)
const unmounts = ref(0)
</script>

<template>
  <LazyComponent
    mode="visible"
    :delay="180"
    @visible-change="visible = $event"
    @load="mounts += 1"
    @unload="unmounts += 1"
  >
    <template #placeholder>
      <div>Waiting for viewport entry...</div>
    </template>

    <ExpensiveChart />
  </LazyComponent>

  <p>Visible: {{ visible }}</p>
  <p>Mounts: {{ mounts }}</p>
  <p>Unmounts: {{ unmounts }}</p>
</template>

LazyComponent is SSR-safe by default: the server render starts from the placeholder and the real slot content mounts only after client-side visibility is known.

LazyComponent Props

propdescriptiondefaulttype
modeMount strategy for the default slot'once''once' | 'visible'
delayTime in milliseconds the component must remain visible before mountinginherited from plugin optionsnumber
observerOptionsIntersectionObserver options for the wrapperinherited rootMargin/threshold defaultsIntersectionObserverInit
tagRoot element tag rendered by LazyComponent'div'string

LazyComponent Events

eventpayloaddescription
visible-changebooleanFires whenever the wrapper enters or leaves the viewport
loadnoneFires when the default slot is mounted
unloadnoneFires when mode="visible" unmounts the default slot

Use css state

There are three states while image loading.
You can take advantage of this feature, make different css controls for different states.

  • loading
  • loaded
  • error
<img src="..." lazy="loading">
<img src="..." lazy="loaded">
<img src="..." lazy="error">
<style>
  img[lazy=loading] {
    /*your style here*/
  }
  img[lazy=error] {
    /*your style here*/
  }
  img[lazy=loaded] {
    /*your style here*/
  }
</style>

Delay loading of images

To avoid loading images that are only shortly visible (e. g. fast scrolling through list of images), a delay in milliseconds can be configured. If a delay is set, an image is only loaded if it stays visible for the specified amount of time.

Set delay in object parameter:

<template>
  <img v-lazy="{ src: 'your image url', loading: 'your loading image url', error: 'your error image url', delay: 500 }">
</template>

๐Ÿ“ Options

keydescriptiondefaulttype
loadingThe image used when the image is loaded-string
errorThe image used when the image failed to load-string
observerOptionsIntersectionObserver options{ rootMargin: '0px', threshold: 0 }IntersectionObserverInit
logDo print debug infotrueboolean
logLevelLog levelerror'error' | 'warn' | 'info' | 'debug' | 'log'
lifecycleSpecify state execution function-Lifecycle
delayTime in milliseconds an image has to stay visible before loading starts0number

โ›ฑ Lifecycle Hooks

keydescription
loadingImage loading
loadedImage loaded
errorImage load error

Contributors

Contributors