Vue-Lazyload

3.0.0 · active · verified Tue Apr 21

Vue-Lazyload is a Vue.js module designed for efficiently lazy-loading images within Vue 3 applications. As of version 3.0.0, it is specifically tailored for Vue 3, offering a lightweight and powerful solution to improve initial page load performance by deferring image loading until they enter the viewport. It provides a simple directive-based API (`v-lazy`) for individual images and supports managing groups of images within a container using `v-lazy-container`. The library offers customization through options for custom loading and error placeholders, configurable pre-loading distances, and various event listeners. While historical versions targeted Vue 1.x and 2.x, the current major release, v3.0.0, represents a significant shift to Vue 3, necessitating changes in application initialization. The project's release cadence appears to be irregular, with notable intervals between major version updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Vue-Lazyload in a Vue 3 application using TypeScript, including global options and examples for the `v-lazy` directive on Vue components and `v-lazy-container` for raw HTML images.

import { createApp } from 'vue';
import App from './App.vue';
import VueLazyload from 'vue-lazyload';

// Import images for placeholders, ensuring they are correctly processed by your bundler
import loadingGif from './assets/loading.gif';
import errorGif from './assets/error.gif';

const app = createApp(App);

app.use(VueLazyload, {
  preLoad: 1.3, // Pre-load 1.3 times the viewport height
  error: errorGif, // Image to show on error
  loading: loadingGif, // Image to show while loading
  attempt: 1 // Number of attempts to load the image
});

app.mount('#app');

// src/App.vue
<template>
  <div>
    <h1>Lazy Loaded Images</h1>
    <div v-for="n in 100" :key="n" class="image-wrapper">
      <img v-lazy="`https://picsum.photos/id/${n + 10}/400/300`" alt="Placeholder Image">
    </div>
    <h2>Lazy Loaded Raw HTML Images</h2>
    <div v-lazy-container="{ selector: 'img', error: '/placeholder_error.jpg', loading: '/placeholder_loading.gif' }" class="raw-html-container">
      <img data-src="https://picsum.photos/id/1000/400/300" alt="Raw HTML Image 1">
      <img data-src="https://picsum.photos/id/1001/400/300" alt="Raw HTML Image 2">
    </div>
  </div>
</template>

<style>
.image-wrapper {
  height: 300px; /* Give some height for scrolling */
  margin-bottom: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f0f0;
}
img {
  max-width: 100%;
  height: auto;
  display: block;
}
.raw-html-container img {
  height: 200px;
  width: 300px;
  object-fit: cover;
  margin: 10px;
}
</style>

view raw JSON →