vue项目实现图片懒加载的简单步骤

作者:Jiang_JY 时间:2024-05-10 14:16:06 

1、安装vue-lazyload插件

npm install vue-lazyload --save-dev

2、在main.js中进行引用

import VueLazyload from "vue-lazyload";

Vue.use(VueLazyload);

//或者自定义配置插件
Vue.use(VueLazyload, {
preLoad: 1.3,
error: require('../src/assets/image/error.png'),
loading: require('../src/assets/image/loading.gif'),
attempt: 1
})

注意:

这里存在一个坑,当图片放在 assets文件下 的时候,要使用上面的这种 require(‘相对路径’)的写法来进行引入。当图片放在 static文件下 的时候就可以直接写路径来进行引入,像下面的写法一样。

static文件图片引入方法:

Vue.use(VueLazyload, {
preLoad: 1.3,
error: '../src/assets/image/error.png',
loading: '../src/assets/image/loading.gif',
attempt: 1
})

各个自定义配置属性含义:

keydescriptiondefaultoptions
preLoad预压高度比例1.3Number
errorsrc of the image upon load fail(指定加载失败时图片)‘data-src’String
loadingsrc of the image while loading(指定加载图片)‘data-src’String
attempt尝试计数3Number
listenEvents想要监听的事件[‘scroll’, ‘wheel’, ‘mousewheel’, ‘resize’, ‘animationend’, ‘transitionend’, ‘touchmove’]Desired Listen Events
adapter动态修改元素属性{ }Element Adapter
filter图片监听或过滤器{ }Image listener filter
lazyComponentlazyload 组件falseLazy Component
dispatchEvent触发dom事件falseBoolean
throttleWaitthrottle wait200Number
observeruse IntersectionObserverfalseBoolean
observerOptionsIntersectionObserver options{ rootMargin: ‘0px’, threshold: 0.1 }IntersectionObserver
silentdo not print debug infotrueBoolean

3、使用(将图片设置为懒加载)

<img v-lazy="psdimg" class="psd" />

注意:

当遇到是v-for循环的时候,或者用div包裹着img的时候,需要在div上面添加 v-lazy-container="{ selector: &lsquo;img&rsquo; }"

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

<!--或者这种:-->

<div v-lazy-container="{ selector: 'img' }" v-html="content">
</div>

如果是这种情况的话,一定要使用 data-src 来指定路径,而不使用v-lazy。因为如果使用的是v-lazy的话,拿到了图片地址也会一直显示不出来。

来源:https://blog.csdn.net/jiangjunyuan168/article/details/124278948

标签:vue,图片懒加载
0
投稿

猜你喜欢

  • YUV转为jpg图像的实现

    2021-07-26 09:18:29
  • 机器学习python实战之决策树

    2021-07-21 12:46:13
  • SQL Server 2012使用Offset/Fetch Next实现分页数据查询

    2024-01-25 03:54:55
  • 谈谈从phpinfo中能获取哪些值得注意的信息

    2023-11-23 23:52:17
  • python使用电子邮件模块smtplib的方法

    2023-09-29 20:20:38
  • Python实现二叉树前序、中序、后序及层次遍历示例代码

    2023-12-02 00:36:40
  • python实现ftp客户端示例分享

    2021-07-06 08:41:18
  • 详解Python 3D引擎Ursina如何绘制立体图形

    2021-11-17 06:04:20
  • python连接mongodb密码认证实例

    2022-09-02 07:08:21
  • Python中的XML库4Suite Server的介绍

    2023-08-12 01:26:41
  • python实现图书借阅系统

    2022-03-26 07:46:32
  • Python中的高级函数map/reduce使用实例

    2021-11-07 06:57:23
  • mysql密码过期导致连接不上mysql

    2024-01-22 12:28:06
  • python 安装教程之Pycharm安装及配置字体主题,换行,自动更新

    2021-01-02 06:34:20
  • PHP实现无限极分类的两种方式示例【递归和引用方式】

    2023-11-15 18:26:33
  • MySQL 移动数据目录后启动失败问题解决

    2024-01-23 02:45:42
  • css元素层叠级别及z-index剖析

    2008-08-29 12:41:00
  • Python+OpenCV内置方法实现行人检测

    2023-10-19 12:52:47
  • python爬虫基础教程:requests库(二)代码实例

    2023-05-31 07:56:35
  • Python实现简单生成验证码功能【基于random模块】

    2022-12-10 18:19:58
  • asp之家 网络编程 m.aspxhome.com