Skip to content

Image 图片

介绍

图片组件用于展示图片,支持加载状态、错误处理、预览功能等。

基础用法

基本使用

最简单的图片展示用法。

vue
<template>
  <yc-image 
    src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
    width="200"
    height="200"
  />
</template>

填充模式

通过 fit 属性设置图片的填充模式。

vue
<template>
  <yc-space>
    <div>
      <p>fill</p>
      <yc-image 
        src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
        width="200"
        height="200"
        fit="fill"
      />
    </div>
    <div>
      <p>contain</p>
      <yc-image 
        src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
        width="200"
        height="200"
        fit="contain"
      />
    </div>
    <div>
      <p>cover</p>
      <yc-image 
        src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
        width="200"
        height="200"
        fit="cover"
      />
    </div>
    <div>
      <p>none</p>
      <yc-image 
        src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
        width="200"
        height="200"
        fit="none"
      />
    </div>
    <div>
      <p>scale-down</p>
      <yc-image 
        src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
        width="200"
        height="200"
        fit="scale-down"
      />
    </div>
  </yc-space>
</template>

加载状态

图片加载过程中会显示加载状态。

vue
<template>
  <yc-space>
    <yc-image 
      src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
      width="200"
      height="200"
      show-loader
    />
    
    <!-- 自定义加载状态 -->
    <yc-image 
      src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
      width="200"
      height="200"
    >
      <template #loader>
        <div style="text-align: center; padding: 50px;">
          <yc-spin />
          <p>加载中...</p>
        </div>
      </template>
    </yc-image>
  </yc-space>
</template>

错误状态

图片加载失败时显示错误状态。

vue
<template>
  <yc-space>
    <!-- 默认错误状态 -->
    <yc-image 
      src="https://invalid-url.com/image.png"
      width="200"
      height="200"
    />
    
    <!-- 自定义错误状态 -->
    <yc-image 
      src="https://invalid-url.com/image.png"
      width="200"
      height="200"
    >
      <template #error>
        <div style="text-align: center; padding: 50px; color: #999;">
          <yc-icon-image-close style="font-size: 24px; margin-bottom: 8px;" />
          <p>加载失败</p>
        </div>
      </template>
    </yc-image>
  </yc-space>
</template>

图片预览

设置 preview 属性开启图片预览功能。

vue
<template>
  <yc-space>
    <yc-image 
      src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
      width="200"
      height="200"
      preview
    />
    
    <!-- 受控的预览 -->
    <yc-image 
      src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
      width="200"
      height="200"
      :preview-visible="previewVisible"
      @preview-visible-change="previewVisible = $event"
    />
    
    <yc-button @click="previewVisible = true">
      预览图片
    </yc-button>
  </yc-space>
</template>

<script setup>
import { ref } from 'vue'

const previewVisible = ref(false)
</script>

图片组预览

使用 ImagePreviewGroup 组件实现多图预览。

vue
<template>
  <yc-image-preview-group>
    <yc-space>
      <yc-image 
        v-for="(src, index) in images"
        :key="index"
        :src="src"
        width="200"
        height="200"
        fit="cover"
        preview
      />
    </yc-space>
  </yc-image-preview-group>
</template>

<script setup>
const images = [
  'https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png',
  'https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png',
  'https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png',
  'https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png'
]
</script>

标题和描述

为图片添加标题和描述信息。

vue
<template>
  <yc-space>
    <yc-image 
      src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
      width="200"
      height="200"
      title="风景图片"
      description="这是一张美丽的风景图片"
    />
    
    <!-- 自定义额外内容 -->
    <yc-image 
      src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
      width="200"
      height="200"
      title="风景图片"
    >
      <template #extra>
        <yc-space>
          <yc-button size="small" type="text">编辑</yc-button>
          <yc-button size="small" type="text">删除</yc-button>
        </yc-space>
      </template>
    </yc-image>
  </yc-space>
</template>

预览操作栏

自定义预览模式下的操作栏。

vue
<template>
  <yc-image 
    src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/placeholder_light.png"
    width="200"
    height="200"
    preview
  >
    <template #preview-actions>
      <yc-image-preview-action name="download" @click="handleDownload">
        <yc-icon-download />
      </yc-image-preview-action>
      <yc-image-preview-action name="share" @click="handleShare">
        <yc-icon-share />
      </yc-image-preview-action>
    </template>
  </yc-image>
</template>

<script setup>
const handleDownload = () => {
  console.log('下载图片')
}

const handleShare = () => {
  console.log('分享图片')
}
</script>

API

Image Props

参数名描述类型默认值
src图片地址string-
width图片宽度number | string-
height图片高度number | string-
title图片标题string-
description图片描述string-
fit图片填充模式'contain' | 'cover' | 'fill' | 'none' | 'scale-down'-
alt图片的替代文字string-
hide-footer是否隐藏底部信息boolean | 'never'false
footer-position底部信息位置'inner' | 'outer''inner'
show-loader是否显示加载状态booleantrue
preview是否开启预览booleanfalse
preview-visible预览是否可见(受控)boolean-
default-preview-visible预览默认是否可见booleanfalse
preview-props预览的配置项ImagePreviewProps-

Image Events

事件名描述参数
preview-visible-change预览显示状态改变时触发(visible: boolean)

Image Slots

插槽名描述
error自定义错误状态
error-icon自定义错误图标
loader自定义加载状态
extra自定义额外内容
preview-actions自定义预览操作栏

ImagePreviewGroup Props

参数名描述类型默认值
src-list图片地址列表string[]-
current当前显示的图片索引number-
default-current默认显示的图片索引number0
infinite是否无限循环booleanfalse
visible预览是否可见boolean-
default-visible预览默认是否可见booleanfalse
mask-closable点击蒙层是否关闭booleantrue
closable是否显示关闭按钮booleantrue
actions-layout操作栏布局string[]['fullScreen', 'rotateRight', 'rotateLeft', 'zoomIn', 'zoomOut', 'originalSize']
esc-to-close按 ESC 键是否关闭booleantrue

ImagePreviewGroup Events

事件名描述参数
change切换图片时触发(index: number)
visible-change预览显示状态改变时触发(visible: boolean)

ImagePreviewGroup Slots

插槽名描述
actions自定义操作栏
default图片内容

类型定义

typescript
export interface ImageProps {
  src?: string;
  width?: number | string;
  height?: number | string;
  title?: string;
  description?: string;
  fit?: ImageObjectFit;
  alt?: string;
  hideFooter?: HideFooter;
  footerPosition?: FooterPosition;
  showLoader?: boolean;
  preview?: boolean;
  previewVisible?: boolean;
  defaultPreviewVisible?: boolean;
  previewProps?: ImagePreviewProps;
}

export type ImageObjectFit = 
  | 'contain' 
  | 'cover' 
  | 'fill' 
  | 'none' 
  | 'scale-down';

export type HideFooter = boolean | 'never';
export type FooterPosition = 'inner' | 'outer';

设计原则

  • 加载体验:提供友好的加载和错误状态
  • 预览功能:支持图片预览和缩放操作
  • 响应式:适配不同尺寸和填充模式
  • 可定制性:支持自定义加载、错误状态和操作栏

注意事项

  1. 图片地址无效时会自动显示错误状态
  2. 预览功能需要设置 preview 属性为 true
  3. 使用 ImagePreviewGroup 可以实现多图预览和切换
  4. fit 属性对应 CSS 的 object-fit 属性
  5. 自定义加载状态时建议保持一致的尺寸避免布局抖动

Released under the MIT License.