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
Table of Contents
- Demo
- Requirements
- Installation
- Usage
- Constructor Options
- Implementation
- Methods
- Authors && Contributors
- License
Demo
Requirements
- Vue.js
1.x
or2.x
Installation
npm
$ npm i vue-lazyload -S
CDN
CDN: https://unpkg.com/vue-lazyload/vue-lazyload.js
Usage
main.js:
Vue // or with optionsVue el: 'body' components: App
template:
use v-lazy-container
work with raw HTML
custom error
and loading
placeholder image
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.
Vue
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
Vue
Element Adapter
Vue
IntersectionObserver
use Intersection Observer to to improve performance of a large number of nodes.
Vue
Lazy Component
Vue;
Implementation
Basic
vue-lazyload will set this img element's src
with imgUrl
string
<!-- with customer error and loading --> <!-- Customer scrollable element --> <!-- srcset -->
CSS state
There are three states while img loading
loading
loaded
error
Methods
Event Hook
vm.$Lazyload.$on(event, callback)
vm.$Lazyload.$off(event, callback)
vm.$Lazyload.$once(event, callback)
$on
Listen for a custom eventsloading
,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
vm$Lazyload
vm.$Lazyload.$once
Arguments:
{string} event
{Function} callback
Example
vm$Lazyload
vm.$Lazyload.$off
If only the event is provided, remove all listeners for that event
Arguments:
{string} event
{Function} callback
Example
{ console}vm$Lazyloadvm$Lazyloadvm$Lazyload
LazyLoadHandler
vm.$Lazyload.lazyLoadHandler
Manually trigger lazy loading position calculation
Example
this$Lazyload
Performance
this$Lazyload
Dynamic switching pictures
<img v-lazy="lazyImg" :key="lazyImg.src">
Authors && Contributors
- hilongjw
- imcvampire
- darrynten
- biluochun
- whwnow
- Leopoldthecoder
- michalbcz
- blue0728
- JounQin
- llissery
- mega667
- RobinCK
- GallenHu