跳转到内容

Image

显示带预览和后备支持的图片。

import { Image } from 'asterui'

基础图片

带 src 和 alt 属性的简单图片显示。

Landscape
import { Image } from 'asterui'

function App() {
  return (
      <Image
        src="/valley.png"
        alt="Landscape"
        width={400}
        height={300}
      />
    )
}

export default App

不同尺寸

各种尺寸的图片。

Small
Medium
Large
import { Image, Space } from 'asterui'

function App() {
  return (
      <Space direction="horizontal" size="md" align="center">
        <Image
          src="/valley.png"
          alt="Small"
          width={100}
          height={100}
        />
        <Image
          src="/valley.png"
          alt="Medium"
          width={200}
          height={150}
        />
        <Image
          src="/valley.png"
          alt="Large"
          width={300}
          height={200}
        />
      </Space>
    )
}

export default App

图片预览

点击图片以全尺寸预览。

Landscape with preview
import { Image } from 'asterui'

function App() {
  return (
      <Image
        src="/valley.png"
        alt="Landscape with preview"
        width={400}
        height={300}
        preview
      />
    )
}

export default App

后备图片

当源加载失败时显示后备图片。

Broken image
import { Image, Space } from 'asterui'

function App() {
  return (
      <Space direction="horizontal" size="md">
        <Image
          src="/does-not-exist.png"
          alt="Broken image"
          width={200}
          height={150}
          fallback="/valley.png"
        />
      </Space>
    )
}

export default App
属性描述类型默认值
src图片源 URLstring-
alt图片的替代文本string''
width图片宽度number | string-
height图片高度number | string-
preview启用点击在灯箱中预览图片booleantrue
fallback源加载失败时的后备图片 URLstring-
placeholder加载时显示的占位符内容React.ReactNode-
onLoad图片成功加载时的回调() => void-
onError图片加载失败时的回调() => void-
className额外的 CSS 类名string-
style内联样式React.CSSProperties-
  • 始终为屏幕阅读器提供有意义的 alt 文本
  • 使用后备图片确保内容始终可见
  • 预览支持键盘访问:按 Enter 或 Space 打开,Escape 关闭
  • 可预览的图片具有 role="button" 和适当的 aria-label
  • 预览模态框具有 role="dialog"aria-modal="true"
  • 错误状态包括描述失败的 aria-label