Skip to content

ImagePreview 图片预览

介绍

一屏展示图片,可放大预览。

函数调用

为了便于使用 ImagePreview,提供了 showImagePreview函数,便于调用 ImagePreview,可设置标识指定一个 ImagePreview 组件调用。

WARNING

因小程序无法动态挂载组件,使用时需要页面存在一个 ImagePreview 组件。

html
<vd-image-preview name="page" />
ts
import { showImagePreview } from 'vital-design';

// 配置同组件 props
showImagePreview({
  name: 'page',
  images: [...],
  active: 2,
});

基础用法

通过 v-model 来设置是否展示,通过 images 属性来设置图片列表。

html
<vd-image-preview v-model="state.show" :images="state.images" />
ts
const state = reactive({
  show: false,
  images: [...],
});

激活位置

通过 active-index 属性来设置激活图片索引。

html
<vd-image-preview
  v-model="state.show"
  :active="state.active"
  :images="state.images"
/>
ts
const state = reactive({
  show: false,
  images: [...],
  active: 2,
});

展示关闭按钮

通过 closeable 属性来展示关闭按钮。

html
<vd-image-preview
  v-model="state.show"
  :images="state.images"
  closeable
/>
ts
const state = reactive({
  show: false,
  images: [...],
});

图片标题

images 项可为 { src, title },标题随当前图片切换;也可用 title 插槽自定义。

html
<vd-image-preview
  v-model="state.show"
  :images="state.images"
  closeable
/>
ts
const state = reactive({
  show: false,
  images: [
    { src: '...', title: '风景一' },
    { src: '...', title: '风景二' },
  ],
});

使用页码插槽

通过 index 插槽来自定义页码。

html
<vd-image-preview
  v-model="state.show"
  :images="state.images"
>
  <template #index="{ active, total }">
    第 {{ active + 1 }} 页
  </template>
</vd-image-preview>
ts
const state = reactive({
  show: false,
  images: [...],
});

使用视图插槽

通过 view 插槽来自定义视图内容,可使用图片、视频或其他自定义内容。

html
<vd-image-preview
  v-model="state.show"
  :images="state.images"
>
  <template #view="{ src, index }">
    <video :poster="src" style="width:100%;"></video>
  </template>
</vd-image-preview>
ts
const state = reactive({
  show: false,
  images: [...],
});

API

Props

参数说明类型默认值
name标识名称string-
v-model是否展示booleanfalse
images图片列表,项可为地址或 { src, title }ImagePreviewImage[][]
active激活索引number-
zoomable是否允许缩放booleantrue
min-zoom最小缩放比例number | string0.3
max-zoom最大缩放比例number | string5
wheel-zoom滚轮单次缩放步进number | string0.1
rotatable是否允许双指旋转booleantrue
show-index是否显示页码booleantrue
closeable是否展示关闭按钮booleanfalse
close-icon关闭按钮图标IconsName'round-close-fill'
close-icon-size关闭按钮图标大小number | string20
close-on-click点击时关闭booleantrue
close-on-click-view点击视图时关闭,优先级高于 closeOnClickbooleantrue
show-menu-by-longpress开启长按图片显示识别小程序码菜单booleanfalse
lock-scroll是否锁定背景滚动booleantrue
z-index层级string | number2001+

代替原始 Props

参数说明类型默认值
custom-id根节点idstring-
custom-class根节点类名ClassValue-
custom-style根节点样式StyleValue-

Events

事件名说明回调参数
click点击时触发MouseEvent
click-close点击关闭按钮时触发MouseEvent
click-view点击视图时触发ImagePreviewOnClickViewEvent
update:model-value展示状态更新时触发boolean
update:active激活索引更新时触发number
open打开时触发-
opened打开动画结束时触发-
close关闭时触发-
closed关闭动画结束时触发-

Slots

名称说明
view预览视图,参数含 src title item index active
cover预览覆盖,参数同 view
title当前图片标题,参数含 title src item active total
index页码
close关闭按钮

Expose

方法名 / 属性名说明参数返回值
open打开预览ImagePreviewOpenOption-
close关闭预览--
swiperRef内部轮播实例-ShallowRef<SwiperInstance | undefined>

类型定义

组件导出以下类型定义:

ts
import type {
  ImagePreviewImage,
  ImagePreviewItem,
  ImagePreviewOpenOption,
  ImagePreviewOnClickViewEvent,
  ImagePreviewExpose,
  ImagePreviewInstance,
} from 'vital-design';

赣ICP备2025061025号-1