Skip to content

Image 图片组件

此组件为uni-app的image组件的加强版,在继承了原有功能外,还支持淡入动画、加载中、加载失败提示、圆角值和形状等。

温馨提示

本项目参考了 uView-Plus 开源项目的组件开发方式,基于 Vue 3 和 TypeScript 实现了自定义组件。目前该组件仍处于测试阶段。
感谢 uView-Plus 开源项目及其团队成员的贡献,他们的组件开发思路为本项目提供了宝贵地参考。如果需要了解更多组件开发细节,可以参考uView-Plus的 image组件 的代码实现。

平台差异说明

APP(vue)H5微信小程序

基本使用示例

html
<!-- 全局使用 -->
<hy-image src="https://q9.itc.cn/q_70/images03/20250211/6ee1b8e0f4704083ba715986c8c3795f.jpeg"></hy-image>
<!-- 单个组件引入 -->
<HyImage src="https://q9.itc.cn/q_70/images03/20250211/6ee1b8e0f4704083ba715986c8c3795f.jpeg"></HyImage>
ts
import { HyImage } from "hy-app"

设置大小

  • 通过配置width设置图片宽度
  • 通过配置height设置图片高度
html
<template>
    <hy-image src="https://q9.itc.cn/q_70/images03/20250211/6ee1b8e0f4704083ba715986c8c3795f.jpeg" width="100" height="100"></hy-image>
</template>

设置圆角

  • 通过配置shape设置图片形状
    • circle:圆形
    • square:方形
html
<template>
    <hy-image src="https://q9.itc.cn/q_70/images03/20250211/6ee1b8e0f4704083ba715986c8c3795f.jpeg" width="100" height="100" shape="circle"></hy-image>
</template>

预览大图

  • 通过配置previewImage设置为true可以预览大图
html
<template>
    <hy-image src="https://q9.itc.cn/q_70/images03/20250211/6ee1b8e0f4704083ba715986c8c3795f.jpeg" previewImage></hy-image>
</template>

设置淡入特效

  • 通过配置fade设置为true可以产生动画(默认为true)
html
<template>
    <hy-image src="https://q9.itc.cn/q_70/images03/20250211/6ee1b8e0f4704083ba715986c8c3795f.jpeg" fade></hy-image>
</template>

API

参数说明类型默认值
src图片地址,强烈建议使用绝对或者网络路径string-
mode裁剪模式,详见image组件stringaspectFill
width宽度,单位任意,如果为数值,默认单位pxstring|number200
height高度,单位任意,如果为数值,默认单位pxstring|number150
shape图片形状[1]circle|squaresquare
radius圆角,默认单位pxstring|number0
lazyLoad是否懒加载,仅微信小程序、App、百度小程序、字节跳动小程序有效booleantrue
showMenuByLongPress是否开启长按图片显示识别小程序码菜单,仅微信小程序有效booleantrue
loadingIcon加载中的图标,或者小图片stringLOADING
errorIcon加载失败的图标,或者小图片stringNOTICE
showLoading是否显示加载中的图标或者自定义的slotbooleantrue
showError是否显示加载错误的图标或者自定义的slotbooleantrue
fade是否需要淡入效果booleantrue
webp只支持网络资源,只对微信小程序有效booleanfalse
duration搭配fade参数的过渡时间,单位msnumber500
bgColor背景颜色,用于深色页面加载图片时,为了和背景色融合string#f3f4f6
previewImage是否预览图片booleanfalse
customStyle定义需要用到的外部样式booleanCSSProperties

Events

事件名说明回调参数
click点击图片时触发-
error图片加载失败时触发err: 错误信息
load图片加载成功时触发e

Slots

插槽名说明接收值
loading自定义加载中的提示内容-
error自定义失败的提示内容-
00:46

  1. circle:两边为半圆;square:方形 ↩︎