Giter Club home page Giter Club logo

vue3-lazyload's Introduction

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
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

vue3-lazyload's People

Contributors

chme avatar github-actions[bot] avatar murongg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.