Vue-Lazyload懒加载

注:此文档中的配置实例中文文档为机翻和根据自我理解进行的翻译
可能如原文有出入,若想理解原意请看英语文档

原官方文档地址:https://www.npmjs.com/package/vue-lazyload

1、安装

npm i vue-lazyload -S

2、使用

2.1、main.js导入并配置


import VueLazyload from 'vue-lazyload'Vue.use(VueLazyload, {preLoad: 1.3,error: 'dist/error.png',       //img的加载中的显示的图片的路径loading: 'dist/loading.gif',    //img加载失败时现实的图片的路径attempt: 1,                   //尝试加载的次数listenEvents['scroll','wheel','mousewheel','resize','animationend','    transitionend','touchmove'],  //你想让vue监听的事件
})new Vue({el: 'app',components: {App}
})
<ul><li v-for="img in list" ><img v-lazy="img.src" ></li>
</ul>

use v-lazy-container work with raw HTML

使用v-lazy-container与原始的HTML工作

<div v-lazy-container="{ selector: 'img' }"><img data-src="//domain.com/img1.jpg"><img data-src="//domain.com/img2.jpg"><img data-src="//domain.com/img3.jpg">
</div>

custom error and loading placeholder image

自定义errorloading占位符图像

<div v-lazy-container="{ selector: 'img', error: 'xxx.jpg', loading: 'xxx.jpg' }"><img data-src="//domain.com/img1.jpg"><img data-src="//domain.com/img2.jpg"><img data-src="//domain.com/img3.jpg">
</div>
<div v-lazy-container="{ selector: 'img' }"><img data-src="//domain.com/img1.jpg" data-error="xxx.jpg"><img data-src="//domain.com/img2.jpg" data-loading="xxx.jpg"><img data-src="//domain.com/img3.jpg">
</div>

3、配置选项

属性 描述 默认值 选项
preLoad 预载高度比例 1.3 Number
error img的加载失败显示的图片的路径 ‘data-src’ String
loading img的加载中的显示的图片的路径 ‘data-src’ String
attempt 尝试次数 3 Number
listenEvents 您想要Vue监听的事件 [‘scroll’, ‘wheel’, ‘mousewheel’, ‘resize’, ‘animationend’, ‘transitionend’, ‘touchmove’] 监听的事件(Desired Listen Events)
adapter 动态修改element的属性 {} 属性对象(Element Adapter)
filter 图像的侦听器过滤器 {} 图像侦听器/过滤器(Image listener filter)
lazyComponent 延迟加载组件 false 懒加载组件(Lazy Component)
dispatchEvent 触发dom事件 false Boolean
throttleWait 200 Number
observer 使用IntersectionObserver false Boolean
observerOptions IntersectionObserver选项 { rootMargin: ‘0px’, threshold: 0.1 } 交叉观察者(IntersectionObserver)
silent 不打印调试信息 true Boolean

4、具体配置示例

4.1、listenEvents

You can configure which events you want vue-lazyload by passing in an array of listener names.

您可以通过传入侦听器名称数组来配置需要vue-lazyload的事件。

Vue.use(VueLazyload, {preLoad: 1.3,error: 'dist/error.png',loading: 'dist/loading.gif',attempt: 1,//默认值为 ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend']listenEvents: [ 'scroll' ]
})

This is useful if you are having trouble with this plugin resetting itself to loading when you have certain animations and transitions taking place

如果您在发生某些动画和转换时,在此插件重置为加载时遇到问题,这是很有用的

4.2、 filter

dynamically modify the src of image
动态修改图像的src

Vue.use(vueLazy, {filter: {progressive (listener, options) {const isCDN = /qiniudn.com/if (isCDN.test(listener.src)) {listener.el.setAttribute('lazy-progressive', 'true')listener.loading = listener.src + '?imageView2/1/w/10/h/10'}},webp (listener, options) {if (!options.supportWebp) returnconst isCDN = /qiniudn.com/if (isCDN.test(listener.src)) {listener.src += '?imageView2/2/format/webp'}}}
})

4.3、adapter

动态修改element的属性

Vue.use(vueLazy, {adapter: {loaded ({ bindType, el, naturalHeight, naturalWidth, $parent, src, loading, error, Init }) {// do something here// example for call LoadedHandlerLoadedHandler(el)},loading (listender, Init) {console.log('loading')},error (listender, Init) {console.log('error')}}
})

4.4、observer与observerOptions

use Intersection Observer to to improve performance of a large number of nodes.

Vue.use(vueLazy, {// set observer to trueobserver: true,// optionalobserverOptions: {rootMargin: '0px',threshold: 0.1}
})

4.5、 lazyComponent

组件懒加载

//main.js
Vue.use(VueLazyload, {lazyComponent: true
});//template
<lazy-component @show="handler"><img class="mini-cover" :src="img.src" width="100%" height="400">
</lazy-component><script>{...methods: {handler (component) {console.log('this component is showing')}}}
</script> 

4.6、设置图片src

vue-lazyload will set this img element’s src with imgUrl string

vue-lazyload会通过imgUrl 字符串对img的src进行设置

<script>
export default {data () {return {imgObj: {src: 'http://xx.com/logo.png',error: 'http://xx.com/error.png',loading: 'http://xx.com/loading-spin.svg'},imgUrl: 'http://xx.com/logo.png' // String}}
}
</script> <template><div ref="container"><img v-lazy="imgUrl"/><div v-lazy:background-image="imgUrl"></div><!-- with customer error and loading --><img v-lazy="imgObj"/><div v-lazy:background-image="imgObj"></div><!-- Customer scrollable element --><img v-lazy.container ="imgUrl"/><div v-lazy:background-image.container="img"></div><!-- srcset --><img v-lazy="'img.400px.jpg'" data-srcset="img.400px.jpg 400w, img.800px.jpg 800w, img.1200px.jpg 1200w"><img v-lazy="imgUrl" :data-srcset="imgUrl' + '?size=400 400w, ' + imgUrl + ' ?size=800 800w, ' + imgUrl +'/1200.jpg 1200w'" /></div>
</template>

4.7、设置CSS状态

There are three states while img loading v

img 加载时的三种状态

loading loaded error

<img src="imgUrl" lazy="loading">
<img src="imgUrl" lazy="loaded">
<img src="imgUrl" lazy="error"><style>img[lazy=loading] {/*your style here*/}img[lazy=error] {/*your style here*/}img[lazy=loaded] {/*your style here*/}/*or background-image*/.yourclass[lazy=loading] {/*your style here*/}.yourclass[lazy=error] {/*your style here*/}.yourclass[lazy=loaded] {/*your style here*/}
</style>

5、方法

vm.$Lazyload.$on(event, callback)

vm.$Lazyload.$off(event, callback)

vm.$Lazyload.$once(event, callback)

  • $on听一个自定义事件loadingloadederror
  • $once收听自定义事件,但仅一次。首次触发时,监听器将被删除。
  • $off 删除事件侦听器。

5.1、$on

vm.$Lazyload.$on

参数

  • {string} event
  • {Function} callback

示例

vm.$Lazyload.$on('loaded', function ({ bindType, el, naturalHeight, naturalWidth, $parent, src, loading, error }, formCache) {console.log(el, src)
})

5.2、$once

vm.$Lazyload.$once

参数

  • {string} event
  • {Function} callback

示例

vm.$Lazyload.$once('loaded', function ({ el, src }) {console.log(el, src)
})

5.3、$off

vm.$Lazyload.$off

If only the event is provided, remove all listeners for that event

如果只提供事件,则删除该事件的所有侦听器

参数

  • {string} event
  • {Function} callback

示例

function handler ({ el, src }, formCache) {console.log(el, src)
}
vm.$Lazyload.$on('loaded', handler)
vm.$Lazyload.$off('loaded', handler)
vm.$Lazyload.$off('loaded')

5.4、延迟加载处理程序

vm.$Lazyload.lazyLoadHandler

Manually trigger lazy loading position calculation

手动触发延迟加载位置计算

示例

this.$Lazyload.lazyLoadHandler()

控制台输出

this.$Lazyload.$on('loaded', function (listener) {console.table(this.$Lazyload.performance())
})

5.5、动态切换图片

 <img v-lazy =“ lazyImg”:key =“ lazyImg.src”>

Vue-Lazyload学习文档相关推荐

  1. 2w多字总结的VUE学习文档

    VUE学习文档 文章目录 VUE学习文档 回顾: 总结 0 目标 1.前言 2.认识Vue 3.快速入门 3.1.创建工程 3.2.安装vue 3.2.1.下载安装 3.2.2.使用CDN 3.3.v ...

  2. IntelliJ IDEA 14,15 使用教程,实战总结,倾囊相授,内附PDF学习文档

    转载博文,尊重原创,感谢前辈分享,原文地址:"请叫我大师兄__"的CSDN博客主页 文章目录 前言 一.安装 二.使用 1.Debug 2.修改内存 3.一般设置 4.高级设置 5 ...

  3. FreeMarker中文帮助手册API文档,基础入门学习文档

    FreeMarker中文帮助手册API文档,基础入门学习文档 分类: 编程技术 发布: bywei 浏览: 7 日期: 2011年5月28日 分享到: QQ空间 新浪微博 腾讯微博 人人网 什么是Fr ...

  4. ffmpeg的中文学习文档

    ffmpeg的中文学习文档 文章目录: 一.ffmpeg介绍 二.学习参考文档 1.中文 一.ffmpeg介绍 ffmpeg是视频处理工具,可选参数非常多,功能也非常的强大,可以用来开发各种视频处理工 ...

  5. Ext JS 6学习文档-第3章-基础组件

    Ext JS 6学习文档-第3章-基础组件 基础组件 在本章中,你将学习到一些 Ext JS 基础组件的使用.同时我们会结合所学创建一个小项目.这一章我们将学习以下知识点: 熟悉基本的组件 – 按钮, ...

  6. Ext JS 6学习文档-第6章-高级组件

    Ext JS 6学习文档-第6章-高级组件 高级组件 本章涵盖了高级组件,比如 tree 和 data view.它将为读者呈现一个示例项目为 图片浏览器,它使用 tree 和 data view 组 ...

  7. NodeJS-001-Nodejs学习文档整理(转-出自http://www.cnblogs.com/xucheng)

    Nodejs学习文档整理 http://www.cnblogs.com/xucheng/p/3988835.html 1.nodejs是什么: nodejs是一个是javascript能在后台运行的平 ...

  8. java学习文档_阿里技术专家带你玩转JVM,从底层源码到项目实战,都在这份文档里...

    作为 Java 的从业者,在找工作的时候,一定会被问及关于 JVM 相关的知识. JVM 知识的掌握程度,在很多面试官眼里是候选人技术深度的一个重要评判标准.而大多数人可能没有对 JVM 的实际开发和 ...

  9. vue 导出word文档(包括图片)

    vue 导出word文档(包括图片) 1.打开终端,安装依赖 -- 安装 docxtemplater npm install docxtemplater pizzip --save-- 安装 jszi ...

  10. [扩展阅读] EasyGUI 学习文档【超详细中文版】

    [扩展阅读] EasyGUI 学习文档[超详细中文版] 0. 安装 EasyGUI 官网:https://github.com/robertlugg/easygui python查看内置的所有模块 h ...

最新文章

  1. 33.搜索插件——autocomplete
  2. 【NOIP2015提高组Day1】 神奇的幻方
  3. MySQL 5.1完全卸载
  4. kdump和crash的配置方法与内核故障原因分析(一)
  5. plaintextedit指定一行一行的高亮显示_LED显示器常见芯片的作用及原理,故障诊断维修...
  6. 无人驾驶的规划与控制(四)——反馈控制
  7. MonGoDB 常见操作, 设置管理员和用户登入
  8. 表结构设计器(EZDML)1.98版公布
  9. win10如何设置任务栏在左侧显示
  10. ubuntu samba
  11. Android多媒体开发框架
  12. 如何共享计算机网络,电脑怎么共享网络给手机上网
  13. python听歌识曲爬虫_【python爬虫】 爬云音乐我和xxx共同听过的歌曲
  14. 25G/100G网卡选购指南
  15. 最全总结 | 聊聊 Python 办公自动化之 Word(中)
  16. Linux下的按键精灵xdotool
  17. 轻松理解 客户端和服务器端的区别
  18. 电脑计算机和用户区分,电脑32位和64位的区别是什么
  19. 三子棋——可修改为多子棋
  20. 贝叶斯网专题1:信息论基础

热门文章

  1. 计算性和复杂度理论2
  2. 将雅虎《心香一脉》每天推荐的文章发到咱邮箱(Java版)
  3. 【kubernetes/k8s源码分析】calico node felix源码分析之一
  4. Fallback class must implement the interface annotated by @FeignClient
  5. 解决 CodeLite 中文乱码的问题,并支持C++11特性
  6. 【react】---redux-actions的基本使用---【巷子】
  7. kafka消费策略 随笔
  8. python笔记本好_如何使用 Python 分析笔记本电脑上的 100 GB 数据
  9. 基于Serverless的流量隐匿(四个方面)
  10. CSS/HTML制作在网页中持续旋转的六面体