3.1.0 • Published 3 years ago

vue-onload v3.1.0

Weekly downloads
88
License
ISC
Repository
github
Last release
3 years ago

vue-onload

A lightweight image loader plugin for Vue.js with Typescript support

Installation

npm install vue-onload --save

How to use

Default use in your main.js Vue project

import * as onLoad from 'vue-onload'
...
createApp(App)
  .use(onLoad.plugin)
...

You can use the directive 'v-onload' in your HTML template

<template>
  <div>
    <img alt="My Image" class="img" v-onload="/static/images/my-image-01.jpg">
  </div>
</template>

Which will be transformed once the resource has been loaded into:

<template>
  <div>
    <img alt="My Image" class="img" width="1024" height="768" src="/static/images/my-image-01.jpg">
  </div>
</template>

A 'data-src' attribute will be added to replace the directive and will be removed once the image is loaded so CSS selector can be used to display smoothly the element like:

img {
  opacity: 1;
  transition: opacity 0.3s;
}

img[data-src] {
  opacity: 0;
}

Preload

You can import preload in your Vue script to preload an array of image sources.

import { ref, onMounted } from 'vue'
import { preload } from 'vue-onload'
export default {
  name: 'MyComponent',

  setup(props, context) {
    const loaded = ref(false)

    onMounted(() => {
      loaded.value = false
      const sources = [
        'https://www.website.com/static/example1.png',
        require('@/assets/img/img-1.jpg'),
        require('@/assets/img/img-2.jpg')
      ]
      preload(sources, (completed, count) => {
        if (completed === true) {
          loaded.value = true
        }
      })
    })

    return {
      loaded
    }
  }
}

License

ISC

3.1.0

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago

0.7.0

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago