• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

浅谈vue中使用图片懒加载vue-lazyload插件详细指南

vue 搞代码 4年前 (2022-01-08) 9次浏览 已收录 0个评论

本篇文章主要介绍了浅谈vue中使用图片懒

本文来源gao!%daima.com搞$代*!码$网3

加载vue-lazyload插件详细指南,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

在vue中使用图片懒加载详细指南分享给大家。具体如下:

说明

当网络请求比较慢的时候,提前给这张图片添加一个像素比较低的占位图片,不至于堆叠在一块,或显示大片空白,让用户体验更好一点。

使用方式

使用vue的 vue-lazyload 插件

插件地址:https://www.npmjs.com/package/vue-lazyload

案例

demo: 懒加载案例demo

Installation 安装方式

npm

 $ npm i vue-lazyload -D

CDN

CDN: https://unpkg.com/vue-lazyload/vue-lazyload.js

 

用法

main.js 在入口文件

 import Vue from 'vue' import App from './App.vue' import VueLazyload from 'vue-lazyload' //引入这个懒加载插件 Vue.use(VueLazyload) // 或者添加VueLazyload 选项 Vue.use(VueLazyload, { preLoad: 1.3, error: 'dist/error.png-600', loading: 'dist/loading.gif', attempt: 1 }) new Vue({ el: 'body', components: { App } }) 

在入口文件添加后,在组件任何地方都可以直接使用把 img 里的:src -> v-lazy

 <div class="pic"> </div>

把之前项目中img 标签里面的 :src 属性 改成 v-lazy 

 <div class="pic"> </div>

参数选项说明

key description default options
preLoad proportion of pre-loading height 1.3 Number
error 当加载图片失败的时候 ‘data-src’ String
loading 当加载图片成功的时候 ‘data-src’ String
attempt 尝试计数 3 Number
listenEvents 想要监听的事件 [‘scroll’, ‘wheel’, ‘mousewheel’, ‘resize’, ‘animationend’, ‘transitionend’, ‘touchmove’] Desired Listen Events
adapter 动态修改元素属性 { } Element Adapter
filter 图片监听或过滤器 { } Image listener filter
lazyComponent lazyload component false Lazy Component
dispatchEvent 触发dom事件 false Boolean
throttleWait throttle wait 200 Number
observer use IntersectionObserver false Boolean
observerOptions IntersectionObserver options { rootMargin: ‘0px’, threshold: 0.1 } IntersectionObserver

想要监听的事件

您可以通过传递数组来配置想要使用vue – lazyload的事件

监听器的名字。

 Vue.use(VueLazyload, { preLoad: 1.3, error: 'dist/error.png-600', loading: 'dist/loading.gif', attempt: 1, // the default is ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend'] listenEvents: [ 'scroll' ] })

如果您遇到这个插件重新设置加载的麻烦,这是很有用的

当你有某些动画和过渡的时候。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是浅谈vue中使用图片懒加载vue-lazyload插件详细指南的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:浅谈vue中使用图片懒加载vue-lazyload插件详细指南

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址