更新记录

1.0.0(2025-10-16) 下载此版本

  • 初始版本发布

平台兼容性

uni-app(4.72)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
×
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟

oui-image

通用的 uniapp 图片加载组件,支持小程序、App、Web 等全平台,提供简洁高效的图片加载体验。

特性

  • 多种显示模式:支持普通图片和背景图模式
  • 骨架屏加载:内置现代化的渐进式加载动画
  • 懒加载支持:可视区域内才加载图片,提升性能
  • 静默错误处理:加载失败时保持界面简洁
  • 备用图片:自动切换备用图片机制
  • 样式定制:支持圆角、边框、阴影等样式

安装

# 通过 uni_modules 安装(推荐)
# 直接将 oui-image 文件夹放入 src/uni_modules/ 目录下即可

快速开始

基础用法

<template>
  <!-- 基础图片 -->
  <oui-image src="/static/images/demo.jpg" width="200px" height="150px" />

  <!-- 圆角图片 -->
  <oui-image src="/static/images/demo.jpg" width="100px" height="100px" radius="8px" />

  <!-- 圆形头像 -->
  <oui-image src="/static/images/avatar.jpg" width="80px" height="80px" radius="50%" />
</template>

图片模式

<template>
  <!-- 不同的图片裁剪模式 -->
  <oui-image src="/static/images/demo.jpg" width="200px" height="150px" image-mode="aspectFit" />
  <oui-image src="/static/images/demo.jpg" width="200px" height="150px" image-mode="aspectFill" />
  <oui-image src="/static/images/demo.jpg" width="200px" height="150px" image-mode="scaleToFill" />
</template>

背景图模式

<template>
  <oui-image
    src="/static/images/banner.jpg"
    mode="background"
    width="100%"
    height="200px"
    radius="12px"
  >
    <view class="content">
      <text class="title">标题内容</text>
      <text class="subtitle">副标题内容</text>
    </view>
  </oui-image>
</template>

<style>
.content {
  padding: 20px;
  color: white;
}
.title {
  font-size: 24px;
  font-weight: bold;
}
.subtitle {
  font-size: 16px;
  opacity: 0.8;
}
</style>

高级功能

懒加载

<template>
  <!-- 懒加载图片 -->
  <oui-image
    src="/static/images/demo.jpg"
    width="100%"
    height="200px"
    lazy-load
    :lazy-offset="100"
  />
</template>

错误处理与备用图片

<template>
  <!-- 带备用图片的错误处理 -->
  <oui-image
    src="/static/images/demo.jpg"
    fallback="/static/images/placeholder.jpg"
    width="200px"
    height="150px"
  />
</template>

样式定制

<template>
  <!-- 自定义样式 -->
  <oui-image
    src="/static/images/demo.jpg"
    width="150px"
    height="150px"
    radius="12px"
    border="2px solid #007aff"
    shadow="0 4px 12px rgba(0, 0, 0, 0.15)"
  />

  <!-- 组合样式效果 -->
  <oui-image
    src="/static/images/demo.jpg"
    width="120px"
    height="120px"
    radius="60px"
    border="3px solid #ffffff"
    shadow="0 8px 24px rgba(0, 122, 255, 0.3)"
  />
</template>

事件处理

<template>
  <oui-image
    src="/static/images/demo.jpg"
    width="200px"
    height="150px"
    @click="handleClick"
    @load="handleLoad"
    @error="handleError"
  />
</template>

<script setup>
const handleClick = (event) => {
  console.log('图片被点击', event);
  uni.showToast({ title: '图片被点击', icon: 'none' });
};

const handleLoad = (event) => {
  console.log('图片加载成功', event.detail);
};

const handleError = (event) => {
  console.log('图片加载失败', event.detail);
  uni.showToast({ title: '图片加载失败', icon: 'error' });
};
</script>

API 文档

Props

属性 类型 默认值 说明
src string - 图片地址(必填)
fallback string - 备用图片地址
width string \| number '100%' 图片宽度
height string \| number '200px' 图片高度
mode 'image' \| 'background' 'image' 显示模式
image-mode string 'aspectFill' 图片裁剪模式
radius string \| number - 圆角大小
border string - 边框样式
shadow string - 阴影样式
show-loading boolean true 是否显示加载状态
lazy-load boolean false 是否启用懒加载
lazy-offset number 50 懒加载偏移量(px)
fade-show boolean true 是否启用淡入效果
webp boolean false 是否支持 WebP 格式
show-menu-by-longpress boolean false 长按是否显示菜单
custom-class string - 自定义样式类
custom-style string - 自定义样式

image-mode 可选值

说明
scaleToFill 缩放图片,使图片完全适应显示区域
aspectFit 缩放图片,使图片的长边能完全显示出来
aspectFill 缩放图片,使图片的短边能完全显示出来,裁剪长边
widthFix 缩放图片,使图片宽度不变,高度自动变化,保持原图宽高比
heightFix 缩放图片,使图片高度不变,宽度自动变化,保持原图宽高比
top 裁剪图片,不缩放图片,只显示图片的顶部区域
bottom 裁剪图片,不缩放图片,只显示图片的底部区域
center 裁剪图片,不缩放图片,只显示图片的中间区域
left 裁剪图片,不缩放图片,只显示图片的左边区域
right 裁剪图片,不缩放图片,只显示图片的右边区域
top left 裁剪图片,不缩放图片,只显示图片的左上边区域
top right 裁剪图片,不缩放图片,只显示图片的右上边区域
bottom left 裁剪图片,不缩放图片,只显示图片的左下边区域
bottom right 裁剪图片,不缩放图片,只显示图片的右下边区域

Events

事件名 参数 说明
click event: Event 点击图片时触发
load event: { detail: { width: string; height: string } } 图片加载成功时触发
error event: { detail: { message: string } } 图片加载失败时触发

Slots

插槽名 说明
default 背景图模式下的内容插槽
loading 自定义加载状态内容(覆盖默认骨架屏)

注意事项

  1. 图片路径:确保图片路径正确,支持相对路径、绝对路径和网络地址
  2. 懒加载性能:启用懒加载时,图片会在进入可视区域时才开始加载,建议为长列表启用
  3. 背景图模式:在背景图模式下,可以在图片上叠加其他内容,适合制作卡片和横幅
  4. 错误处理:组件采用静默错误处理,建议设置备用图片提升用户体验
  5. 样式优先级:自定义样式会覆盖组件默认样式,请注意样式优先级

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

暂无用户评论。