vue中使用圖片懶載入vue-lazyload外掛指南
阿新 • • 發佈:2018-12-11
使用方式
使用vue的 vue-lazyload 外掛 外掛地址:
https://www.npmjs.com/package/vue-lazyload
Installation 安裝方式
1. npm
$ npm i vue-lazyload -D
2.CDN
<script src="https://unpkg.com/vue-lazyload/vue-lazyload.js"></script>
<script>
Vue.use(VueLazyload)
...
</script>
3.用法
入口檔案 main.js
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 }) new Vue({ el: 'body', components: { App } })
4.在入口檔案新增後,在元件任何地方都可以直接使用把 img 裡的:src -> v-lazy
<div class="pic"> <a href="#"><img :src="'/static/img/' + item.productImage" alt=""></a> </div> 把之前專案中img 標籤裡面的 :src 屬性 改成 v-lazy <div class="pic"> <a href="#"><img v-lazy="'/static/img/' + item.productImage" alt=""></a> </div>
5.引數選賢說明
6.想要監聽的事件
您可以通過傳遞陣列來配置想要使用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' ]
})