Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
V

vue-lazyload

> 前端框架
Open source

A Vue.js plugin for lazyload your Image or Component in your application.

8.0K stars0 likes0 views
WebsiteGitHub

About

A Vue.js plugin for lazyload your Image or Component in your application.

Vue-Lazyload

Vue module for lazyloading images in your applications. Some of goals of this project worth noting include:

  • Be lightweight, powerful and easy to use
  • Work on any image type
  • Add loading class while image is loading
  • Supports both of Vue 1.0 and Vue 2.0

For Vue 3

Please use [email protected], see here

Table of Contents

  • Demo
  • Requirements
  • Installation
  • Usage
  • Constructor Options
  • Implementation
    • Basic
    • Css state
  • Methods
    • Event hook
    • LazyLoadHandler
    • Performance
  • Authors && Contributors
  • License

Demo

Demo

Requirements

  • Vue.js 1.x or 2.x

Installation

npm

bash

$ npm i vue-lazyload -S

yarn

bash

$ yarn add vue-lazyload

CDN

CDN: https://unpkg.com/vue-lazyload/vue-lazyload.js

xml
<script src="https://unpkg.com/vue-lazyload/vue-lazyload.js"></script>
<script>
  Vue.use(VueLazyload)
  ...
</script>

Usage

main.js:

javascript

import Vue from 'vue'
import App from './App.vue'
import VueLazyload from 'vue-lazyload'

Vue.use(VueLazyload)

// or with options
const loadimage = require('./assets/loading.gif')
const errorimage = require('./assets/error.gif')

Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: errorimage,
  loading: loadimage,
  attempt: 1
})

new Vue({
  el: 'body',
  components: {
    App
  }
})

template:

xml
<ul>
  <li v-for="img in list">
    
  </li>
</ul>

use v-lazy-container work with raw HTML

xml

custom error and loading placeholder image

xml
xml

Constructor Options

key description default options
preLoad proportion of pre-loading height 1.3 Number
error src of the image upon load fail 'data-src' String
loading src of the image while loading 'data-src' String
attempt attempts count 3 Number
listenEvents events that you want vue listen for ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove'] Desired Listen Events
adapter dynamically modify the attribute of element { } Element Adapter
filter the image's listener filter { } Image listener filter
lazyComponent lazyload component false Lazy Component
dispatchEvent trigger the dom event false Boolean
throttleWait throttle wait 200 Number
observer use IntersectionObserver false Boolean
observerOptions IntersectionObserver options { rootMargin: '0px', threshold: 0.1 } IntersectionObserver
silent do not print debug info true Boolean

Desired Listen Events

You can configure which events you want vue-lazyload by passing in an array of listener names.

javascript
Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: 'dist/error.png',
  loading: 'dist/loading.gif',
  attempt: 1,
  // the default is ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend']
  listenEvents: [ 'scroll' ]
})

This is useful if you are having trouble with this plugin resetting itself to loading when you have certain animations and transitions taking place

Image listener filter

dynamically modify the src of image

javascript
Vue.use(vueLazy, {
    filter: {
      progressive (listener, options) {
          const isCDN = /qiniudn.com/
          if (isCDN.test(listener.src)) {
              listener.el.setAttribute('lazy-progressive', 'true')
              listener.loading = listener.src + '?imageView2/1/w/10/h/10'
          }
      },
      webp (listener, options) {
          if (!options.supportWebp) return
          const isCDN = /qiniudn.com/
          if (isCDN.test(listener.src)) {
              listener.src += '?imageView2/2/format/webp'
          }
      }
    }
})

Element Adapter

javascript
Vue.use(vueLazy, {
    adapter: {
        loaded ({ bindType, el, naturalHeight, naturalWidth, $parent, src, loading, error, Init }) {
            // do something here
            // example for call LoadedHandler
            LoadedHandler(el)
        },
        loading (listender, Init) {
            console.log('loading')
        },
        error (listender, Init) {
            console.log('error')
        }
    }
})

IntersectionObserver

use Intersection Observer to to improve performance of a large number of nodes.

javascript
Vue.use(vueLazy, {
  // set observer to true
  observer: true,

  // optional
  observerOptions: {
    rootMargin: '0px',
    threshold: 0.1
  }
})

Lazy Component

javascript
Vue.use(VueLazyload, {
  lazyComponent: true
});
xml
<lazy-component @show="handler">
  
</lazy-component>

<script>
  {
    ...
    methods: {
      handler (component) {
        console.log('this component is showing')
      }
    }

  }
</script>

Use in list

xml
<lazy-component v-for="(item, index) in list" :key="item.src" >
  
</lazy-component>

Implementation

Basic

vue-lazyload will set this img element's src with imgUrl string

xml
<script>
export default {
  data () {
    return {
      imgObj: {
        src: 'http://xx.com/logo.png',
        error: 'http://xx.com/error.png',
        loading: 'http://xx.com/loading-spin.svg'
      },
      imgUrl: 'http://xx.com/logo.png' // String
    }
  }
}
</script>

<template>
  

     
     
     

     
     
     

    
    
    
  </div>
</template>

CSS state

There are three states while img loading

loading loaded error

xml
xml
<style>
  img[lazy=loading] {
    /*your style here*/
  }
  img[lazy=error] {
    /*your style here*/
  }
  img[lazy=loaded] {
    /*your style here*/
  }
  /*
  or background-image
  */
  .yourclass[lazy=loading] {
    /*your style here*/
  }
  .yourclass[lazy=error] {
    /*your style here*/
  }
  .yourclass[lazy=loaded] {
    /*your style here*/
  }
</style>

Methods

Event Hook

vm.$Lazyload.$on(event, callback) vm.$Lazyload.$off(event, callback) vm.$Lazyload.$once(event, callback)

  • $on Listen for a custom events loading, loaded, error
  • $once Listen for a custom event, but only once. The listener will be removed once it triggers for the first time.
  • $off Remove event listener(s).

vm.$Lazyload.$on

Arguments:

  • {string} event
  • {Function} callback

Example

javascript
vm.$Lazyload.$on('loaded', function ({ bindType, el, naturalHeight, naturalWidth, $parent, src, loading, error }, formCache) {
  console.log(el, src)
})

vm.$Lazyload.$once

Arguments:

  • {string} event
  • {Function} callback

Example

javascript
vm.$Lazyload.$once('loaded', function ({ el, src }) {
  console.log(el, src)
})

vm.$Lazyload.$off

If only the event is provided, remove all listeners for that event

Arguments:

  • {string} event
  • {Function} callback

Example

javascript
function handler ({ el, src }, formCache) {
  console.log(el, src)
}
vm.$Lazyload.$on('loaded', handler)
vm.$Lazyload.$off('loaded', handler)
vm.$Lazyload.$off('loaded')

LazyLoadHandler

vm.$Lazyload.lazyLoadHandler

Manually trigger lazy loading position calculation

Example

javascript

this.$Lazyload.lazyLoadHandler()

Performance

javascript
this.$Lazyload.$on('loaded', function (listener) {
  console.table(this.$Lazyload.performance())
})

Dynamic switching pictures

xml
 

Authors && Contributors

  • hilongjw
  • imcvampire
  • darrynten
  • biluochun
  • whwnow
  • Leopoldthecoder
  • michalbcz
  • blue0728
  • JounQin
  • llissery
  • mega667
  • RobinCK
  • GallenHu

License

The MIT License

Issues· 198 open

View all issuesOpen on GitHub
  • #521

    水平滚动时懒加载图片功能没法实现呀

    Updated Jul 4, 2025
  • #349

    Lazyload not firing on switching v-show, unless Scrolled by a little...

    Updated Jan 17, 2025
  • #471

    why execute this.lazyLoadHandler twice

    Updated Jan 7, 2025
  • #362

    google调试disable cache 图片会重复加载两次。不禁用缓存没有问题

    Updated Oct 22, 2024
  • #498

    My image called two times

    Updated Oct 22, 2024
  • #518

    vue 3.5+ 会报错 TypeError: Cannot read properties of null (reading 'getBoundingClientRect')

    Updated Oct 14, 2024
  • #514

    Solution to Nuxt3 getSSRProps Error

    Updated Sep 24, 2024
  • #507

    nuxt3下将v-lazy认为是vue指令,导致服务端SSR启用。

    Updated Aug 25, 2024
  • #478

    Problem when loading page with v-for

    Updated Jul 5, 2024
  • #517

    v-lazy元素被复用。导致图片出现在错误的元素上。

    Updated Jun 24, 2024

> Tags

JavaScriptlazyloadvuevue-lazyload

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架