vue-lazyload
A Vue.js plugin for lazyload your Image or Component in your application.
Top Related Projects
High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.
🔥 Highly performant, light ~1kb and configurable lazy loader in pure JS with no dependencies for responsive images, iframes and more
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Vanilla JavaScript plugin for lazyloading images
A fast, flexible, and small SEO-friendly lazy loader.
Hey, be lazy! bLazy.JS is a lightweight pure JavaScript script for lazy loading and multi-serving images. It's working in all modern browsers including IE7+.
Quick Overview
Vue-lazyload is a Vue.js plugin for lazy loading images and components in Vue applications. It helps improve performance by loading images only when they enter the viewport, reducing initial page load time and conserving bandwidth.
Pros
- Easy integration with Vue.js applications
- Customizable loading and error placeholders
- Supports both images and components lazy loading
- Provides various options for fine-tuning lazy loading behavior
Cons
- May not be necessary for small applications with few images
- Requires additional setup compared to standard image loading
- Can potentially cause layout shifts if not implemented carefully
- Limited browser support for older versions (IE11 and below)
Code Examples
- Basic image lazy loading:
<template>
<img v-lazy="'https://example.com/image.jpg'" />
</template>
- Lazy loading with custom loading and error placeholders:
<template>
<img
v-lazy="{
src: 'https://example.com/image.jpg',
loading: '/path/to/loading.gif',
error: '/path/to/error.png'
}"
/>
</template>
- Lazy loading a component:
<template>
<div v-lazy-container="{ selector: '.lazy' }">
<heavy-component class="lazy"></heavy-component>
</div>
</template>
Getting Started
- Install the package:
npm install vue-lazyload
- Import and use the plugin in your Vue app:
import Vue from 'vue'
import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload, {
preLoad: 1.3,
error: 'dist/error.png',
loading: 'dist/loading.gif',
attempt: 1
})
- Use the
v-lazy
directive in your components:
<template>
<img v-lazy="'https://example.com/image.jpg'" />
</template>
Competitor Comparisons
High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.
Pros of lazysizes
- Framework-agnostic, works with any JavaScript project
- More comprehensive features, including LQIP (Low Quality Image Placeholder) support
- Larger community and more frequent updates
Cons of lazysizes
- Larger file size, which may impact page load times
- Steeper learning curve due to more extensive configuration options
- Not specifically optimized for Vue.js projects
Code Comparison
vue-lazyload:
<template>
<img v-lazy="imageSrc" />
</template>
<script>
export default {
data: () => ({ imageSrc: 'path/to/image.jpg' })
}
</script>
lazysizes:
<img data-src="path/to/image.jpg" class="lazyload" />
<script>
import 'lazysizes';
</script>
Summary
While lazysizes offers a more comprehensive and framework-agnostic solution for lazy loading images, vue-lazyload provides a simpler, Vue-specific implementation. lazysizes excels in features and community support but may be overkill for smaller Vue.js projects. vue-lazyload integrates seamlessly with Vue.js applications, offering a more straightforward implementation at the cost of some advanced features.
🔥 Highly performant, light ~1kb and configurable lazy loader in pure JS with no dependencies for responsive images, iframes and more
Pros of lozad.js
- Framework-agnostic, can be used with any JavaScript project
- Smaller bundle size (1.9KB gzipped) compared to vue-lazyload
- Supports IntersectionObserver API for better performance
Cons of lozad.js
- Lacks Vue-specific optimizations and integration
- Requires more manual setup for Vue projects
- Fewer built-in features compared to vue-lazyload (e.g., no error handling)
Code Comparison
vue-lazyload:
Vue.use(VueLazyload, {
preLoad: 1.3,
error: 'error.png',
loading: 'loading.gif',
attempt: 1
})
lozad.js:
const observer = lozad();
observer.observe();
The code snippets demonstrate that vue-lazyload is more tightly integrated with Vue and offers more configuration options out of the box, while lozad.js provides a simpler, more generic implementation that requires additional setup for Vue projects.
vue-lazyload is specifically designed for Vue.js applications, offering seamless integration and Vue-specific optimizations. It provides built-in error handling, loading states, and attempts configuration. However, it has a larger bundle size and is limited to Vue projects.
lozad.js, being framework-agnostic, offers more flexibility and a smaller footprint. It leverages the IntersectionObserver API for improved performance but requires more manual setup in Vue projects and lacks some of the Vue-specific features provided by vue-lazyload.
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Pros of vanilla-lazyload
- Framework-agnostic, can be used with any JavaScript project
- Supports native lazy loading for modern browsers
- More customizable options for fine-tuning lazy loading behavior
Cons of vanilla-lazyload
- Requires more manual setup compared to vue-lazyload
- May have a steeper learning curve for Vue.js developers
- Lacks Vue-specific optimizations and integrations
Code Comparison
vue-lazyload:
Vue.use(VueLazyload, {
preLoad: 1.3,
error: 'error.png',
loading: 'loading.gif',
attempt: 1
})
vanilla-lazyload:
const lazyLoadInstance = new LazyLoad({
elements_selector: ".lazy",
threshold: 300,
callback_enter: (element) => {
// Custom logic
}
});
Both libraries offer easy-to-use implementations, but vue-lazyload integrates more seamlessly with Vue.js projects. vanilla-lazyload provides more granular control over the lazy loading process, making it suitable for a wider range of applications. While vue-lazyload is optimized for Vue.js, vanilla-lazyload's framework-agnostic nature allows for greater flexibility across different project types.
Vanilla JavaScript plugin for lazyloading images
Pros of lazyload
- Framework-agnostic, can be used with any JavaScript project
- Supports both images and iframes
- Smaller bundle size (8.5KB vs 18.6KB for vue-lazyload)
Cons of lazyload
- Less Vue-specific features and optimizations
- Fewer customization options for loading and error states
- May require more manual setup in Vue projects
Code Comparison
lazyload:
const lazyload = new LazyLoad({
elements_selector: ".lazy",
threshold: 300
});
vue-lazyload:
Vue.use(VueLazyload, {
preLoad: 1.3,
error: 'error.png',
loading: 'loading.gif',
attempt: 1
});
Key Differences
- lazyload is a vanilla JavaScript library, while vue-lazyload is specifically designed for Vue.js applications
- vue-lazyload offers more Vue-specific features like custom directives and component integration
- lazyload provides a more general-purpose solution that can be used across different frameworks and vanilla JavaScript projects
- vue-lazyload has more built-in options for handling loading and error states, making it easier to implement in Vue applications
Both libraries aim to improve performance by lazy loading images and other media, but they cater to different use cases and project requirements.
A fast, flexible, and small SEO-friendly lazy loader.
Pros of yall.js
- Framework-agnostic, can be used with any JavaScript project
- Supports lazy loading of both images and videos
- Smaller bundle size (around 1KB gzipped)
Cons of yall.js
- Lacks Vue-specific optimizations and integration
- Doesn't offer as many customization options for Vue projects
- May require more manual setup in Vue applications
Code Comparison
vue-lazyload usage in a Vue component:
<template>
<img v-lazy="imageUrl" />
</template>
<script>
export default {
data() {
return {
imageUrl: 'https://example.com/image.jpg'
}
}
}
</script>
yall.js usage in a Vue component:
<template>
<img class="lazy" data-src="https://example.com/image.jpg" src="placeholder.jpg" />
</template>
<script>
import yall from 'yall-js'
export default {
mounted() {
yall()
}
}
</script>
Both libraries provide lazy loading functionality, but vue-lazyload offers a more Vue-centric approach with its directive, while yall.js requires a bit more setup but can be used in any JavaScript environment. vue-lazyload provides more Vue-specific features and optimizations, while yall.js offers a lighter-weight solution that can be used across different frameworks.
Hey, be lazy! bLazy.JS is a lightweight pure JavaScript script for lazy loading and multi-serving images. It's working in all modern browsers including IE7+.
Pros of blazy
- Framework-agnostic, can be used with any JavaScript project
- Smaller file size (around 2KB minified and gzipped)
- Supports both images and iframes
Cons of blazy
- Less Vue-specific features and integration
- Fewer customization options for loading effects
- May require more manual setup in Vue projects
Code Comparison
blazy:
var bLazy = new Blazy({
selector: 'img',
offset: 100,
success: function(element){
console.log("Element loaded");
}
});
vue-lazyload:
Vue.use(VueLazyload, {
preLoad: 1.3,
error: 'dist/error.png',
loading: 'dist/loading.gif',
attempt: 1
})
Summary
blazy is a lightweight, framework-agnostic lazy loading library that supports both images and iframes. It has a smaller file size and can be used in various JavaScript projects. However, it may require more manual setup in Vue projects and offers fewer Vue-specific features compared to vue-lazyload.
vue-lazyload, on the other hand, is specifically designed for Vue.js applications, providing seamless integration and Vue-specific features. It offers more customization options for loading effects but is limited to Vue projects and has a larger file size.
Choose blazy for a lightweight, versatile solution across different frameworks, or vue-lazyload for a more integrated and feature-rich experience in Vue applications.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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 vue-lazyload@3.x, see here
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
yarn
$ yarn add vue-lazyload
CDN
CDN: https://unpkg.com/vue-lazyload/vue-lazyload.js
<script src="https://unpkg.com/vue-lazyload/vue-lazyload.js"></script>
<script>
Vue.use(VueLazyload)
...
</script>
Usage
main.js:
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:
<ul>
<li v-for="img in list">
<img v-lazy="img.src" >
</li>
</ul>
use v-lazy-container
work with raw HTML
<div v-lazy-container="{ selector: 'img' }">
<img data-src="//domain.com/img1.jpg">
<img data-src="//domain.com/img2.jpg">
<img data-src="//domain.com/img3.jpg">
</div>
custom error
and loading
placeholder image
<div v-lazy-container="{ selector: 'img', error: 'xxx.jpg', loading: 'xxx.jpg' }">
<img data-src="//domain.com/img1.jpg">
<img data-src="//domain.com/img2.jpg">
<img data-src="//domain.com/img3.jpg">
</div>
<div v-lazy-container="{ selector: 'img' }">
<img data-src="//domain.com/img1.jpg" data-error="xxx.jpg">
<img data-src="//domain.com/img2.jpg" data-loading="xxx.jpg">
<img data-src="//domain.com/img3.jpg">
</div>
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.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
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
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.
Vue.use(vueLazy, {
// set observer to true
observer: true,
// optional
observerOptions: {
rootMargin: '0px',
threshold: 0.1
}
})
Lazy Component
Vue.use(VueLazyload, {
lazyComponent: true
});
<lazy-component @show="handler">
<img class="mini-cover" :src="img.src" width="100%" height="400">
</lazy-component>
<script>
{
...
methods: {
handler (component) {
console.log('this component is showing')
}
}
}
</script>
Use in list
<lazy-component v-for="(item, index) in list" :key="item.src" >
<img class="mini-cover" :src="item.src" width="100%" height="400">
</lazy-component>
Implementation
Basic
vue-lazyload will set this img element's src
with imgUrl
string
<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 ref="container">
<img v-lazy="imgUrl"/>
<div v-lazy:background-image="imgUrl"></div>
<!-- with customer error and loading -->
<img v-lazy="imgObj"/>
<div v-lazy:background-image="imgObj"></div>
<!-- Customer scrollable element -->
<img v-lazy.container ="imgUrl"/>
<div v-lazy:background-image.container="img"></div>
<!-- srcset -->
<img v-lazy="'img.400px.jpg'" data-srcset="img.400px.jpg 400w, img.800px.jpg 800w, img.1200px.jpg 1200w">
<img v-lazy="imgUrl" :data-srcset="imgUrl' + '?size=400 400w, ' + imgUrl + ' ?size=800 800w, ' + imgUrl +'/1200.jpg 1200w'" />
</div>
</template>
CSS state
There are three states while img loading
loading
loaded
error
<img src="imgUrl" lazy="loading">
<img src="imgUrl" lazy="loaded">
<img src="imgUrl" lazy="error">
<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 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.$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
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
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
this.$Lazyload.lazyLoadHandler()
Performance
this.$Lazyload.$on('loaded', function (listener) {
console.table(this.$Lazyload.performance())
})
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
License
Top Related Projects
High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.
🔥 Highly performant, light ~1kb and configurable lazy loader in pure JS with no dependencies for responsive images, iframes and more
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Vanilla JavaScript plugin for lazyloading images
A fast, flexible, and small SEO-friendly lazy loader.
Hey, be lazy! bLazy.JS is a lightweight pure JavaScript script for lazy loading and multi-serving images. It's working in all modern browsers including IE7+.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot