Skip to content

xtf-image

组件说明

xtf-image 是增强图片组件,在原生 image 基础上增加了加载状态、错误重试、占位图、遮罩文字、标签、徽标、水印和图片预览等功能,适用于商品图片、头像和内容展示等场景。


基础用法

1. 基础用法

通过 src 设置图片地址:

vue
<template>
  <xtf-image src="/static/banner.jpg" width="100%" height="360" radius="20" />
</template>

2. 圆形与圆角

通过 circle 设置圆形,通过 radius 设置圆角:

vue
<template>
  <view style="display: flex; gap: 24rpx">
    <xtf-image src="/static/avatar.jpg" width="120" height="120" circle />
    <xtf-image src="/static/product.jpg" width="200" height="200" radius="20" />
  </view>
</template>

3. 带标签与遮罩

通过 tagText 显示标签,通过 captiondesc 显示遮罩文字:

vue
<template>
  <xtf-image
    src="/static/product.jpg"
    width="100%"
    height="400"
    tag-text="新品"
    tag-theme="danger"
    caption="限时特惠"
    desc="剩余3天"
  />
</template>

4. 占位图与懒加载

通过 placeholder 设置占位图,通过 lazy 启用懒加载:

vue
<template>
  <xtf-image
    src="/static/large-image.jpg"
    width="100%"
    height="500"
    placeholder="/static/placeholder.png"
    lazy
  />
</template>

全部属性

属性类型默认值作用描述适用范围
srcString''图片地址图片
widthString | Number'100%'宽度尺寸
heightString | Number220高度尺寸
ratioString | Number''宽高比尺寸
modeString'aspectFill'裁剪模式模式
radiusString | Number28圆角样式
radiusTopLeft / radiusTopRight / radiusBottomRight / radiusBottomLeftString | Number''分别覆盖四个角的圆角值非对称圆角
circleBooleanfalse是否圆形形状
shapeString'rounded'形状形状
variantString'default'变体样式
borderedBooleanfalse是否显示边框边框
grayscaleBooleanfalse是否灰度样式
lazyBooleanfalse是否懒加载加载
fadeShowBooleantrue是否淡入动画
showLoadingBooleantrue是否显示加载加载
showErrorBooleantrue是否显示错误错误
errorTextString'加载失败,点击重试'加载失败时显示的提示文案错误态文案
placeholder / fallbackString''加载占位图、失败备用图片加载
lazyLoad / webp / showMenuByLongpressBooleanundefined / false / false原生图片能力开关图片
previewList / previewCurrentArray / String | Number[] / 0预览集合和当前图片预览
previewBooleanfalse是否可预览预览
captionString''遮罩标题遮罩
descString''遮罩描述遮罩
tagTextString''标签文字标签
tagThemeString'primary'标签主题标签
tagVariantString'solid'标签视觉变体标签样式
badgeValueString | Number''徽标值徽标
badgeTextString''徽标文字文本徽标
badgeDotBooleanfalse是否显示点状徽标红点提醒
badgeTheme / badgeVariantString'danger' / 'bubble'徽标主题与视觉变体徽标样式
watermarkString''水印文字水印
overlayAlignString'left'遮罩文字对齐方式遮罩排版
retryableBooleantrue错误态是否允许点击重试错误恢复
customClassString''自定义类名样式覆盖
customStyleString | Object''自定义样式动态样式覆盖

事件

事件名称触发时机回调参数参数说明
load图片加载完成时触发(event)加载事件
error图片加载失败时触发(event)错误事件
retry重试加载时触发--
click点击时触发(event)点击事件对象
longpress长按时触发(event)长按事件对象
preview预览图片前触发(payload){ current, urls, src }

事件与插槽使用示例

下例同时监听加载、失败、重试、点击和预览事件,并以 erroroverlaytop 插槽覆盖对应区域:

vue
<template>
  <xtf-image
    src="/static/product.jpg"
    :preview-list="previewList"
    preview
    caption="夏日新品"
    @load="onLoad"
    @error="onError"
    @retry="onRetry"
    @click="onClick"
    @longpress="onLongpress"
    @preview="onPreview"
  >
    <template #error><xtf-text text="图片加载失败,点击重试" color="secondary" /></template>
    <template #overlay><xtf-text text="夏日新品" color="inverse" /></template>
    <template #top><xtf-tag label="新品" theme="danger" size="sm" /></template>
  </xtf-image>
</template>

<script>
export default {
  data() {
    return { previewList: ['/static/product.jpg', '/static/product-2.jpg'] }
  },
  methods: {
    onLoad(event) {
      console.log('图片加载完成:', event)
    },
    onError(event) {
      console.log('图片加载失败:', event)
    },
    onRetry() {
      console.log('重新加载图片')
    },
    onClick(event) {
      console.log('点击图片:', event)
    },
    onLongpress(event) {
      console.log('长按图片:', event)
    },
    onPreview(payload) {
      console.log('预览图片:', payload.current)
    }
  }
}
</script>

插槽

插槽名称说明
error自定义错误状态
overlay自定义遮罩内容
top自定义顶部标签/徽标

主题说明

  • 加载状态使用骨架屏动画
  • 错误状态使用 xtf-iconxtf-text 组件
  • 标签使用 xtf-tag 组件
  • 徽标使用 xtf-badge 组件

如需全局调整主题,请在 xtf-config-provider 或主题配置中覆盖相关 CSS 变量。

MIT Licensed