在vue中使用圖片懶加載vue-lazyload插件
使用方式
使用vue的 vue-lazyload 插件
插件地址:
https://www.npmjs.com/package/vue-lazyload
Installation 安裝方式
npm
$ npm i vue-lazyload -D
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' //引入這個(gè)懶加載插件 Vue.use(VueLazyload) // 或者添加VueLazyload 選項(xiàng)Vue.use(VueLazyload, { loading:"/static/loading-svg/loading-bars.svg" }) new Vue({ el: 'body', components: { App }})
在入口文件添加后医舆,在組件任何地方都可以直接使用把 img 里的:src -> v-lazy
<div class="pic"> <a href="#"><img :src="'/static/img/' + item.productImage" alt=""></a></div> 把之前項(xiàng)目中img 標(biāo)簽里面的 :src 屬性 改成 v-lazy <div class="pic"> <a href="#"><img v-lazy="'/static/img/' + item.productImage" alt=""></a></div>
參數(shù)選項(xiàng)說(shuō)明
key | description | default | options |
---|---|---|---|
preLoad |
proportion of pre-loading height | 1.3 |
Number |
error |
當(dāng)加載圖片失敗的時(shí)候 | 'data-src' |
String |
loading |
當(dāng)加載圖片成功的時(shí)候 | 'data-src' |
String |
attempt |
嘗試計(jì)數(shù) | 3 |
Number |
listenEvents |
想要監(jiān)聽的事件 | ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove'] |
Desired Listen Events |
adapter |
動(dòng)態(tài)修改元素屬性 | { } |
Element Adapter |
filter |
圖片監(jiān)聽或過(guò)濾器 | { } |
Image listener filter |
lazyComponent |
lazyload component | false |
Lazy Component |
dispatchEvent |
觸發(fā)dom事件 | false |
Boolean |
throttleWait |
throttle wait | 200 |
Number |
observer |
use IntersectionObserver | false |
Boolean |
observerOptions |
IntersectionObserver options | { rootMargin: '0px', threshold: 0.1 } | IntersectionObserver |
想要監(jiān)聽的事件
您可以通過(guò)傳遞數(shù)組來(lái)配置想要使用vue - lazyload的事件
監(jiān)聽器的名字。
Vue.use(VueLazyload, { preLoad: 1.3, loading:"/static/loading-svg/loading-bars.svg" attempt: 1, // the default is ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend'] listenEvents: [ 'scroll' ]})