0.4.1 • Published 1 year ago

cube-vue-image-lazy v0.4.1

Weekly downloads
31
License
MIT
Repository
github
Last release
1 year ago

cube-vue-image-lazy

A super simple image lazy loader for Vue.

Install

yarn add cube-vue-image-lazy

Warning: You'll need to install the w3c Intersection Observer polyfill in case you're targeting a browser which doesn't support it.

Usage

You can register the component globally so it's available in all your apps:

import Vue from 'vue'
import ImageLazy from 'cube-vue-image-lazy'

Vue.use(ImageLazy)

// Or you can override defaults
Vue.use(ImageLazy, {
  name: 'ImageLazyLoad',
  delay: 500,
  baseClass: 'image-lazy-load',
  deferredClass: 'image-lazy-load-deferred',
  loadingClass: 'image-lazy-load-loading',
  loadedClass: 'image-lazy-load-loaded'
})

Or use it locally in any of your components:

<template>
  <div id="App">
    <ImageLazy
      class="photo"
      src="https://source.unsplash.com/random/200x200"
      srcset="https://source.unsplash.com/random/400x400 2x"
      baseClass="image-lazy"
      deferredClass="image-lazy-deferred"
      loadingClass="image-lazy-loading"
      loadedClass="image-lazy-loaded"
      :delay="500"
      @loading="loading = true"
      @load="loaded = true"
    />
  </div>
</template>

<script>
  import ImageLazy from 'cube-vue-image-lazy'

  export default {
    components: {
      ImageLazy
    },
    data() {
      return {
        loading: false,
        loaded: false
      }
    }
  }
</script>

<style>
  .image-lazy {
    opacity: 0;
  }
  .image-lazy-loading {
    filter: blur(5px);
    transform: translateY(1rem);
  }
  .image-lazy-loaded {
    transition: opacity 2s ease, transform 1s ease, filter 1s ease;
    opacity: 1;
    transform: none;
    filter: none;
  }
</style>

Props

NameRequiredTypeDefaultDescription
eagerfalseBooleanfalseDo not wait the image appears in the viewport and loads the image immediately.
delayfalseNumber0The delay in ms before showing up the image.
baseClassfalseString'image-lazy'The name of the class always added to the image.
deferredClassfalseString'image-lazy-deferred'The name of the class added while the loading of the image is deferred.
loadingClassfalseString'image-lazy-loading'The name of the class added while the image is loading.
loadedClassfalseString'image-lazy-loaded'The name of the class added when the image is loaded.

Events

NameDescription
loadingThe image is loading.
loadThe image is loaded.

Development Setup

# Project setup
yarn install

# Compiles and hot-reloads for development – Run the demo
yarn serve

# Compiles and minifies for production
yarn build

# Builds the npm ready packages
yarn bundle

# Watches for file changes and builds the npm ready packages accordingly
yarn watch

# Lints and fixes files
yarn lint

# Run the unit tests
yarn test:unit
0.4.1

1 year ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago