码迷,mamicode.com
首页 > 其他好文 > 详细

vue项目中实现图片懒加载的方法

时间:2018-12-03 14:06:30      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:路径   过多   height   ttl   span   adapter   use   scroll   body   

对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面内未出现在可视区域内的图片先不做加载, 等到滚动到可视区域后再去加载。这样子对于页面加载性能上会有很大的提升,也提高了用户体验。

实现方法(使用vue的vue-lazyload插件)

1.安装插件

npm install vue-lazyload --save-dev

2.在入口文件main.js中引入并使用

import VueLazyload from vue-lazyload

直接使用

Vue.use(VueLazyload)

或者添加自定义选项

Vue.use(VueLazyload, {
preLoad: 1.3,
error: dist/error.png,
loading: dist/loading.gif,
attempt: 1
})

3.修改图片显示方式为懒加载(将 :src 属性直接改为v-lazy)

<a href="javascript:;"><img v-lazy="‘/static/img/‘ + item.productImage"></a>

参数选项说明

图片懒加载的简单效果已经实现了,然后就可以按这开发文档的api进行扩展了:

keydescriptiondefaultoptions
preLoad proportion of pre-loading height(预加载高度比例) 1.3 Number
error src of the image upon load fail(图片路径错误时加载图片) ‘data-src‘ String
loading src of the image while loading(预加载图片) ‘data-src‘ String
attempt attempts count(尝试加载图片数量) 3 Number
listenEvents

events that you want vue listen for

(想要监听的vue事件)

默认[‘scroll‘]可以省略,

当插件跟页面中的动画或过渡等事件有冲突是,

可以尝试其他选项

[‘scroll‘(默认),

‘wheel‘,

‘mousewheel‘,

‘resize‘,

‘animationend‘,

‘transitionend‘,

‘touchmove‘]

Desired Listen Events
adapter

dynamically modify the attribute of element

(动态修改元素属性)

{ } Element Adapter
filter the image‘s listener filter(动态修改图片地址路径) { } Image listener filter
lazyComponent lazyload component false Lazy Component
dispatchEvent trigger the dom event false Boolean
throttleWait throttle wait 200 Number
observer use IntersectionObserver false Boolean
observerOptions IntersectionObserver options { rootMargin: ‘0px‘, threshold: 0.1 } IntersectionObserver

vue项目中实现图片懒加载的方法

标签:路径   过多   height   ttl   span   adapter   use   scroll   body   

原文地址:https://www.cnblogs.com/xieli26/p/10057763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!