vue-lazyload簡單使用vue
npm地址:https://www.npmjs.com/package/vue-lazyloadgit
github地址:https://github.com/hilongjw/vue-lazyloadgithub
例子:http://hilongjw.github.io/vue-lazyload/npm
1.安裝插件spa
npm install vue-lazyload --save
或者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>
2.引用文件,通常在main.js全局引用,且配置好圖片code
main.js:component
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 } })
3. vue文件中將須要懶加載的圖片綁定 v-bind:src 修改成 v-lazy blog
template:圖片
<ul>
<li v-for="img in list">
<img v-lazy="img.src" >
</li>
</ul>