Skip to content

ImagePreview 图片预览

AuImagePreview 是从桌面编辑器场景抽离出的通用查看器,支持多图切换、滚轮与双指缩放、放大后拖拽、旋转、键盘控制、焦点恢复和滚动锁定。图片项既可传 URL 字符串,也可传 { src, alt, title } 对象。

基础用法

多图预览

点击缩略图打开;方向键切图,+ / - 缩放,0 还原,Escape 关闭。

vue
<template>
  <div class="image-preview-demo">
    <button
      v-for="(image, index) in images"
      :key="image.title"
      class="image-preview-demo__thumbnail au-focus-ring"
      type="button"
      @click="showPreview(index)"
    >
      <img :src="image.src" :alt="image.alt" />
      <span>{{ image.title }}</span>
    </button>
  </div>
  <AuImagePreview
    v-if="visible"
    v-model="visible"
    :images="images"
    :initial-index="initialIndex"
  />
</template>
<script setup>
import { ref } from 'vue';
import { AuImagePreview } from 'aurora-plus';
const visible = ref(false);
const initialIndex = ref(0);
const images = [
  createDemoImage('湖蓝', '#3478f6', '#dbe8ff'),
  createDemoImage('松绿', '#2f9e72', '#d9f2e8'),
  createDemoImage('暖橙', '#d98a24', '#f9ead3'),
];
function createDemoImage(title, color, background) {
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="960" height="600" viewBox="0 0 960 600"><rect width="960" height="600" rx="32" fill="${background}"/><circle cx="480" cy="270" r="150" fill="${color}"/><text x="480" y="500" text-anchor="middle" font-family="system-ui" font-size="48" fill="${color}">${title}</text></svg>`;
  return {
    src: `data:image/svg+xml;charset=UTF-8,${encodeURIComponent(svg)}`,
    alt: `${title}示例图`,
    title,
  };
}
function showPreview(index) {
  initialIndex.value = index;
  visible.value = true;
}
</script>
<style scoped>
.image-preview-demo {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}
.image-preview-demo__thumbnail {
  display: grid;
  gap: 6px;
  padding: 0;
  border: 0;
  color: var(--au-color-text-secondary);
  background: transparent;
  font: inherit;
  font-size: var(--au-font-size-small);
  cursor: zoom-in;
}
.image-preview-demo__thumbnail img {
  display: block;
  width: 100%;
  aspect-ratio: 8 / 5;
  border: 1px solid var(--au-material-border);
  border-radius: var(--au-radius-control);
  object-fit: cover;
}
</style>

Attributes

属性说明默认值
v-model显示状态;组件直接挂载时默认显示true
images / urlList图片对象或 URL 列表;images 优先[]
initialIndex / infinite初始索引 / 循环切换0 / true
initialScale打开和切图后的缩放1
zoomRate / minScale / maxScale缩放倍率与范围1.2 / 0.2 / 5
rotateStep单次旋转角度90
fit图片适配方式,同 object-fitcontain
wheelZoom滚轮缩放true
hideOnClickModal点击图片之外的舞台关闭false
closeOnPressEscape / lockScrollEscape 关闭 / 锁定页面滚动true
showToolbar / showProgress工具条 / 图片序号true
teleported / appendTo是否传送及目标true / body
topOffset顶部保留空间,桌面自定义标题栏可传 400
zIndex / ariaLabel层级 / 无障碍名称10000 / 图片预览

Events、Slots 与 Exposes

事件包括 openopenedclose(reason)closedswitch(index, image)zoom(scale, source)rotate(degree, direction)loaderrorupdate:modelValue

插槽包括 toolbarprogresserrorempty。组件暴露 open()close()setActiveItem(index)showPrevious()showNext()zoomIn()zoomOut()setScale()rotateLeft()rotateRight()resetTransform()activeIndexscaleviewerRef

Aurora Plus · Vue 3 Component Library