源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-11-08 18:43 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:浅谈vue中使用图片懒加载vue-lazyload插件详细指南
在vue中使用图片懒加载详细指南[b],[/b]分享给大家。具体如下: [b]说明 [/b] 当网络请求比较慢的时候,提前给这张图片添加一个像素比较低的占位图片,不至于堆叠在一块,或显示大片空白,让用户体验更好一点。 [b]使用方式[/b] 使用vue的 vue-lazyload 插件 插件地址:[url=https://www.npmjs.com/package/vue-lazyload]https://www.npmjs.com/package/vue-lazyload[/url] [b]案例[/b] demo: [url=http://hilongjw.github.io/vue-lazyload/]懒加载案例demo [/url] [b]Installation 安装方式[/b] npm
$ npm i vue-lazyload -D
[b]CDN[/b] CDN: [url=https://unpkg.com/vue-lazyload/vue-lazyload.js]https://unpkg.com/vue-lazyload/vue-lazyload.js[/url]
<script src="https://unpkg.com/vue-lazyload/vue-lazyload.js"></script>
<script>
 Vue.use(VueLazyload)
 ...
</script>
[b]用法[/b] 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',
 loading: 'dist/loading.gif',
 attempt: 1
})

new Vue({
 el: 'body',
 components: {
  App
 }
})
在入口文件添加后,在组件任何地方都可以直接使用把 img 里的:src -> v-lazy
 <div class="pic">
  <a href="#" rel="external nofollow" rel="external nofollow" ><img :src="'/static/img/' + item.productImage" alt=""></a>
</div>
把之前项目中img 标签里面的 :src 属性 改成 v-lazy 
 <div class="pic">
  <a href="#" rel="external nofollow" rel="external nofollow" ><img v-lazy="'/static/img/' + item.productImage" alt=""></a>
</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'] [url=https://segmentfault.com/a/1190000011672452#desired-listen-events]Desired Listen Events[/url]
adapter 动态修改元素属性 { } [url=https://segmentfault.com/a/1190000011672452#element-adapter]Element Adapter[/url]
filter 图片监听或过滤器 { } [url=https://segmentfault.com/a/1190000011672452#image-listener-filter]Image listener filter[/url]
lazyComponent lazyload component false [url=https://segmentfault.com/a/1190000011672452#lazy-component]Lazy Component[/url]
dispatchEvent 触发dom事件 false Boolean
throttleWait throttle wait 200 Number
observer use IntersectionObserver false Boolean
observerOptions IntersectionObserver options { rootMargin: '0px', threshold: 0.1 } [url=https://segmentfault.com/a/1190000011672452#intersectionobserver]IntersectionObserver[/url]
[b]想要监听的事件[/b] 您可以通过传递数组来配置想要使用vue - lazyload的事件 监听器的名字。
Vue.use(VueLazyload, {
 preLoad: 1.3,
 error: 'dist/error.png',
 loading: 'dist/loading.gif',
 attempt: 1,
 // the default is ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend']
 listenEvents: [ 'scroll' ]
})
如果您遇到这个插件重新设置加载的麻烦,这是很有用的 当你有某些动画和过渡的时候。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部