Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vue3-lazyload

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue3-lazyload

A Vue3.x image lazyload plugin

  • 0.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10K
increased by1.99%
Maintainers
1
Weekly downloads
 
Created
Source

vue3-lazyload



A vue3.x image lazyload plugin.

🚀 Features

  • 0 dependencies: No worry about your bundle size
  • 🦾 Type Strong: Written in Typescript
  • 💪 Small Size: Only 4kb
  • 🌎 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>

📁 Options

keydescriptiondefaulttype
loadingThe image used when the image is loaded-string
errorThe image used when the image failed to load-string
observerOptionsIntersectionObserver options{ rootMargin: '0px', threshold: 0.1 }IntersectionObserverInit
logDo not print debug infotrueboolean
lifecycleSpecify state execution function-Lifecycle

⛱ Lifecycle Hooks

keydescription
loadingImage loading
loadedImage loaded
errorImage load error

📄 TODO

  • Migrate to typescript
  • rollup
  • eslint
  • overall unit tests
  • *.d.ts
  • Perfect type
  • lifecycle
  • commitlint & husky
  • LazyComponent
  • LazyImage
  • LazyContainer
  • Perfect example

FAQs

Package last updated on 24 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc