• Stars
    star
    165
  • Rank 228,906 (Top 5 %)
  • Language
    TypeScript
  • Created about 4 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Vue3.x image lazyload plugin

vue3-lazyload



A vue3.x image lazyload plugin.

🚀 Features

  • 0 dependencies: No worry about your bundle size
  • 🦾 Type Strong: Written in Typescript
  • 🌎 Browser support: Use it through CDN
  • 😊 Support Hook: useLazyload

📎 Installation

$ npm i vue3-lazyload
# or
$ yarn 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

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>

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

Have to be aware of is v-lazy don't use v-lazy="lazyOptions", in this case, vue cannot monitor data changes.

<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('/example/assets/logo.png')
    const lazyRef = useLazyload(src, {
      lifecycle: {
        loading: () => {
          console.log('loading')
        },
        error: () => {
          console.log('error')
        },
        loaded: () => {
          console.log('loaded')
        }
      }
    })
    return {
      lazyRef
    }
  }
}
</script>

<template>
  <img ref="lazyRef" class="image" width="100">
</template>

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

key description default type
loading The image used when the image is loaded - string
error The image used when the image failed to load - string
observerOptions IntersectionObserver options { rootMargin: '0px', threshold: 0.1 } IntersectionObserverInit
log Do print debug info true boolean
logLevel Log level error 'error' | 'warn' | 'info' | 'debug' | 'log'
lifecycle Specify state execution function - Lifecycle
delay Time in milliseconds an image has to stay visible before loading starts 0 number

⛱ Lifecycle Hooks

key description
loading Image loading
loaded Image loaded
error Image load error

Contributors

Contributors

More Repositories

1

meme

在线表情包制作工具
TypeScript
11
star
2

awesome-vue-composables

👉 Awesome Vue Composables Resources
7
star
3

imagetracer

Simple raster image tracer and vectorizer written in TypeScript by imagetracer.js.
TypeScript
6
star
4

koa-typescript-cms

基于TypeScript编写的koa cms框架
TypeScript
6
star
5

star-ui

A vue 3.x components libary
SCSS
5
star
6

command-workflow

Configurable command workflow that executes specific tasks based on commands. Simple, flexible, and easily expandable.
TypeScript
5
star
7

vue-color-next

🎨 Vue Color Picker Component for Vue 3.x
TypeScript
5
star
8

FunCode

FunCode 课件
4
star
9

hooks-date

Reactive date hooks for vue
Vue
4
star
10

vue-reconstruct

✈️ A upgrade vue codemod. Help you quickly upgrade from Vue2.x to Vue3.x !
TypeScript
3
star
11

v-lottie

Vue composables for Lottie
TypeScript
3
star
12

image2file

Simple image / svg / psd / file convert.
TypeScript
3
star
13

go-leetcode

leetcode算法
Go
2
star
14

JavaScript-leetcode-learn

JavaScript算法与数据结构学习
JavaScript
2
star
15

mr-progress-vue

The progress component for Vue
Vue
2
star
16

win11vue

Win11 UI for vue
TypeScript
2
star
17

beautylog

🍬 Terminal beautiful log for Deno
TypeScript
2
star
18

react-drag-tree-table

📖 React draggable tree table component
TypeScript
2
star
19

flutter_shop

学习flutter商城demo
Dart
1
star
20

go-arithmetic

go 基础算法练习
Go
1
star
21

music-rank-crawler

毕业设计,爬取各音乐网站排行榜
TypeScript
1
star
22

start-ts

murong's start-ts template
TypeScript
1
star
23

vscode-vuejs-challenges

Vuejs challenges in VS Code.
TypeScript
1
star
24

deno_starter

TypeScript
1
star
25

v-promise

A Vue hook, Simplify promise operation
TypeScript
1
star
26

vethers

ethers of vue-next hooks
TypeScript
1
star
27

vite-plugin-basic-css-selector

Add a basic selector to each CSS selector by Vite plugin.
TypeScript
1
star
28

compiler-core

Vue compiler-core for Go
Go
1
star
29

eslint-config

JavaScript
1
star
30

Programmer-Ebook

自己收集的程序员各类电子书
1
star
31

My-Blog-For-Hexo

JavaScript
1
star
32

unplugin-preset-vue

Integrate useful preset for unplugin vue
TypeScript
1
star
33

murongg

1
star
34

cnpn

😄 Check to see if your npm package name is registered
TypeScript
1
star