vue-lazyload

Vue-Lazyload

一款适于在应用中懒加载图片的vue插件,下面是一些值得注意的地方:

  • 轻量、功能强大且易于使用
  • 可作用于任意一种图片
  • 在图片加载过程中添加加载样式
  • 同时支持Vue 1.0和Vue 2.0

Demo

Demo

要求

  • Vue.js 1.x 或者 2.x

安装

npm

$ npm install vue-lazyload

CDN

CDN: https://unpkg.com/vue-lazyload/vue-lazyload.js

<script src="https://unpkg.com/vue-lazyload/vue-lazyload.js"></script>
<script>
  Vue.use(VueLazyload)
  ...
</script>

使用

main.js

import Vue from 'vue'
import App from './App.vue'
import VueLazyload from 'vue-lazyload'

Vue.use(VueLazyload)

// or with options
Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: 'dist/error.png',
  loading: 'dist/loading.gif',
  attempt: 1
})

new Vue({
  el: 'body',
  components: {
    App
  }
})

构造选项options

keydescriptiondefaultoptions
preLoadproportion of pre-loading height1.3Number
errorsrc of the image upon load fail'data-src'String
loadingsrc of the image while loading'data-src'String
attemptattempts count3Number
listenEventsevents that you want vue listen for['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove']Desired Listen Events
adapterdynamically modify the attribute of element{ }Element Adapter
filterthe image’s src filter{ }Image url filter
lazyComponentlazyload componentfalseLazy Component

想要监听的事件

通过传递给监听器事件的名字,你可以决定哪些事件可以触发懒加载。

Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: 'dist/error.png',
  loading: 'dist/loading.gif',
  attempt: 1,
  // the default is ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend']
  listenEvents: [ 'scroll' ]
})

Image url filter

动态的修改图片路径。

Vue.use(vueLazy, {
    filter: {
      webp ({ src }) {
          const isCDN = /qiniudn.com/
          if (isCDN.test(src)) {
              src += '?imageView2/2/format/webp'
          }
          return src
      },
      someProcess ({ el, src }) {
        if (el.getAttribute('large')) {
          src += '?large'
        }
        return src
      }
    }
})

Element Adapter

Vue.use(vueLazy, {
    adapter: {
        loaded ({ bindType, el, naturalHeight, naturalWidth, $parent, src, loading, error, Init }) {
            // do something here
            // example for call LoadedHandler
            LoadedHandler(el)
        },
        loading (listender, Init) {
            console.log('loading')
        },
        error (listender, Init) {
            console.log('error')
        }
    }
})

Lazy Component

<lazy-component @show="handler">
  <img class="mini-cover" :src="img.src" width="100%" height="400">
</lazy-component>

<script>
  {
    ...
    methods: {
      handler (component) {
        console.log('this component is showing')
      }
    }

  }
</script>

执行

基础

vue-lazyload会将这个img元素的src设置为imgUrl字符串。

<script>
export default {
  data () {
    return {
      imgObj: {
        src: 'http://xx.com/logo.png',
        error: 'http://xx.com/error.png',
        loading: 'http://xx.com/loading-spin.svg'
     }
     imgUrl: 'http://xx.com/logo.png' // String
    }
  }
}
</script>

<template>
  <div ref="container">
     <img v-lazy="imgUrl"/>
     <div v-lazy:background-image="imgUrl"></div>

     <!-- with customer error and loading -->
     <img v-lazy="imgObj"/>
     <div v-lazy:background-image="imgObj"></div>

     <!-- Customer scrollable element -->
     <img v-lazy.container ="imgUrl"/>
     <div v-lazy:background-image.container="img"></div>

    <!-- srcset -->
    <img v-lazy="'img.400px.jpg'" data-srcset="img.400px.jpg 400w, img.800px.jpg 800w, img.1200px.jpg 1200w">
    <img v-lazy="imgUrl" :data-srcset="imgUrl' + '?size=400 400w, ' + imgUrl + ' ?size=800 800w, ' + imgUrl +'/1200.jpg 1200w'" />
  </div>
</template>

CSS state

在图片加载时,样式有三种状态,分别是loading、loaded和error

<img src="imgUrl" lazy="loading">
<img src="imgUrl" lazy="loaded">
<img src="imgUrl" lazy="error">

<style>
  img[lazy=loading] {
    /*your style here*/
  }
  img[lazy=error] {
    /*your style here*/
  }
  img[lazy=loaded] {
    /*your style here*/
  }
  /*
  or background-image
  */
  .yourclass[lazy=loading] {
    /*your style here*/
  }
  .yourclass[lazy=error] {
    /*your style here*/
  }
  .yourclass[lazy=loaded] {
    /*your style here*/
  }
</style>

方法

事件钩子

vm.$Lazyload.$on(event, callback) 
vm.$Lazyload.$off(event, callback) 
vm.$Lazyload.$once(event, callback)
  • $on监听普通事件loading、loaded和error
  • $once监听事件,但是只有一次
  • $off移除监听器

参数

{string} event
{Function} callback

例子

vm.$Lazyload.$on('loaded', function ({ bindType, el, naturalHeight, naturalWidth, $parent, src, loading, error }, formCache) {
  console.log(el, src)
})

vm.$Lazyload.$once('loaded', function ({ el, src }) {
  console.log(el, src)
})

function handler ({ el, src }, formCache) {
  console.log(el, src)
} 
vm.$Lazyload.$on('loaded', handler)
vm.$Lazyload.$off('loaded', handler)
vm.$Lazyload.$off('loaded')

懒加载处理器

vm.$Lazyload.lazyLoadHandler

手动触发懒加载位置计算。

this.$Lazyload.lazyLoadHandler()

原文链接

https://github.com/HeliumLau/vue-lazyload/blob/master/README.md

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值