1.首先 需要 引入第三的插件库
npm install vue-lazyload --save-dev
2.在入口文件
import VueLazyLoad from 'vue-lazyload'
Vue.use(VueLazyLoad)
// 如果不设置 loading 那么预加载的时候显示的图片就是它本身data-src 路径下的图片,所以在这我就没设置其它的实属性了
3.组件内对背景图片实现懒加载
<img v-lazy="'/static/images/toLeft.png'" @click="goPage('operatLeaderLeft')" class="imgLeft">
<img v-lazy="'/static/images/toRight.png'" @click="goPage('operatLeaderRight')" class="imgRight">
<img v-lazy="'/static/images/back.png'" @click="goPage('operatHome')" class="imgBack" >
// 对应的 样式
.bgColor .imgLeft{
cursor:pointer;
transform:scale(0.4);
position: absolute;
left: -2.5%;
top: 40%;
overflow: hidden;
}
.bgColor .imgRight{
cursor:pointer;
transform:scale(0.4);
position: absolute;
right: -2.5%;
top: 40%;
overflow: hidden;
}
.bgColor .imgBack{
cursor:pointer;
transform:scale(0.4);
position: absolute;
right: -2.5%;
bottom: 3%;
overflow: hidden;
}
*效果图是
.bgColor .imgLeft{
cursor:pointer;
transform:scale(0.4);
position: absolute;
left: 0%;
top: 40%;
overflow: hidden;
}
效果如下
// 显示在上图位置
left: 0%;
// 下面会显示和右边图标的位置
left:-2.5%
// 问题来了 ,可能是位置导致加载不来,但是为什么同样的代码右侧的却能一直出来
right: -2.5% 效果如上图
.bgColor .imgLeft{
cursor:pointer;
transform:scale(0.4);
position: absolute;
right: 92.5%;
top: 40%;
overflow: hidden;
}
效果图
left:5% 改成 right:95%
data(){
return{
imgUrl: require('/assets/xxx.png')
}
}
<img v-lazy="imgUrl" @click="goPage('operatLeaderRight')" class="imgRight">
如果是在和src 平级的static文件夹下,可以直接写图片路径,如下
<img v-lazy="'/static/images/toRight.png'" @click="goPage('operatLeaderRight')" class="imgRight">
因篇幅问题不能全部显示,请点此查看更多更全内容