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 {
  transform: translateX(100vh);
}
.image-lazy-loaded {
  transition: opacity 1s ease;
  opacity: 1;
}
</style>

Props

Name Required Type Default Description
delay false Number 0 The delay before loading the image when it appears in the viewport.
baseClass false String 'image-lazy' The name of the class always added to the image.
deferredClass false String 'image-lazy-deferred' The name of the class added while the loading of the image is deferred.
loadingClass false String 'image-lazy-loading' The name of the class added while the image is loading.
loadedClass false String 'image-lazy-loaded' The name of the class added when the image is loaded.

Events

Name Description
loading The image is loading.
load The 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

GitHub