vue-lazyload 插件的使用及基础

vue-lazyload使用地址 链接

命令行安装

npm install vue-lazyload --save-dev

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>

在入口文件中引用

import Vue from 'vue'
import App from './App.vue'
import VueLazyload from 'vue-lazyload'  //引入这个懒加载插件

Vue.use(VueLazyload)

// 或者添加VueLazyload 选项
Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: 'dist/error.png',
  loading: 'dist/loading.gif',
  attempt: 1
})

页面使用

<img v-lazy="require('@/assets/images/home/img2_1.png')" alt="" />

或封装的全局方法 $getImgSrc 引入图片,因为vue中循环本地的图片时候 @/assets/img + 图片路径 是无法显示的
<img v-lazy="$getImgSrc('home','img2_1.png')" alt="" />

在vue-cli中 需要用require 如果有src同级的static文件则直接引用

keydescriptiondefaultoptions
preLoad proportion of pre-loading height 1.3 Number
error 当加载图片失败的时候 'data-src' String
loading 当加载图片成功的时候 'data-src' String
attempt 尝试计数 3 Number
listenEvents 想要监听的事件 ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove'] Desired Listen Events
adapter 动态修改元素属性 { } Element Adapter
filter 图片监听或过滤器 { } Image listener filter
lazyComponent lazyload component false Lazy Component
dispatchEvent 触发dom事件 false Boolean
throttleWait throttle wait 200 Number
observer use IntersectionObserver false Boolean
observerOptions IntersectionObserver options { rootMargin: '0px', threshold: 0.1 } IntersectionObserver

想要监听的事件,您可以通过传递数组来配置想要使用vue - lazyload的事件  监听器的名字。

 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' ]
})
原文地址:https://www.cnblogs.com/mary-123/p/12402929.html