vue-lazyload图片延迟加载插件

一、安装

npm

npm install vue-lazyload --save-dev

CDN

CDN:

< script src = “ https://unpkg.com/vue-lazyload/vue-lazyload.js ” > < / script > 

< script >

  Vue 。使用( VueLazyload )

  ...

</ script > 

二、用法

在main.js里面import这个包,而且在使用的时候可以对其进行配置

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
  }
})

配置完成后,将img标签中的src改成v-lazy即可。

三、配置解析

ydescriptiondefaultoptions
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 listener filter{ }Image listener filter
lazyComponentlazyload componentfalseLazy Component
dispatchEventtrigger the dom eventfalseBoolean
throttleWaitthrottle wait200Number
observeruse IntersectionObserverfalseBoolean
observerOptionsIntersectionObserver options{ rootMargin: '0px', threshold: 0.1 }IntersectionObserver
silentdo not print debug infotrueBoolean

四、图片的加载状态

图片的加载状态由三种:loading、loaded、error

这三种状态可以结合CSS样式来展示:

<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> 

五、原理

参考博文

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值