main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload,{
error:'./static/resources/1.jpg',
loading:'./static/resources/2.gif',
})
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>',
})
index.vue
<template>
<div class="index">
<div>
<span class="text">图片懒加载测试</span>
</div>
<div class="imgBox">
<div class="imgItem"><img class="w100Pct" v-lazy="imgPath1" /></div>
<div class="imgItem"><img class="w100Pct" v-lazy="imgPath2" /></div>
<div class="imgItem"><img class="w100Pct" v-lazy="imgPath3" /></div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
imgPath1: "/static/resources/5.jpg", //图片在static文件夹下面
imgPath2: "/static/resources/3.jpg",
imgPath3: "/static/resources/4.jpg"
};
}
};
</script>
<style lang="scss" scoped>
img[lazy="loading"] {
// 加载中图片样式
width: 32px !important;
height: 32px !important;
}
img[lazy="error"] {
// error图片样式
}
img[lazy="loaded"] {
// 已加载图片样式
}
// sass 定义的变量
$w100Pct: 100%;
$h100Pct: 100%;
.index {
width: 1200px;
height: 600px;
}
// sass样式嵌套
.imgBox {
width: $w100Pct;
height: $h100Pct;
background-color: #a51;
.w100Pct {
width: $w100Pct;
height: $h100Pct;
}
.imgItem {
display: flex;
align-items: center;
justify-content: center;
width: $w100Pct;
height: $h100Pct;
}
}
</style>
资源文件(图片)在static下
实现效果(加载中)
实现效果(加载完成)