v-lazy图片懒加载
1、安装插件
npm install vue-lazyload --save2、在main.js文件中注册插件
import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload, {
loading: require('img/loading.png'),//加载中图片,一定要有,不然会一直重复加载占位图
error: require('img/error.png') //加载失败图片
});配置参数:
| key | description | default |
|---|---|---|
| preLoad | 提前加载高度(数字 1 表示 1 屏的高度) | 1.3 |
| error | 图片加载失败时显示的图片 | 'data-src' |
| loading | 图片加载状态下显示的图片 | 'data-src' |
| attempt | 加载错误后最大尝试次数 | 3 |
| listenEvents | 监听事件 | ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove']` |
| adapter | 动态修改元素属性 | { } |
| filter | 图片监听过滤 | { } |
| lazyComponent | 组件懒加载 | false |
| dispatchEvent | 触发元素状态监听事件(error, loaded, rendered) | false |
3、v-lazy有三种状态
lazy=loaded
lazy=error
lazy=loading4、在图片中使用
img:
<img v-lazy="img_url" :key='img_url'> //将 :src 属性直接改为v-lazy, :key是为了防止刷新页面或图片更改时图片不更新背景图:
<div v-lazy:background-image="{src: url}"></div>如果不想再loading或者error时显示兜底图片,可以分别设置样式代替:
<style>
img[lazy=loading] { }
img[lazy=error] { }
img[lazy=loaded] { }
</style>本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!