ImagePreview
Custom preview with zoom, navigation and guarded closing. The function API prefers native platform preview.
Import
With uni-app easycom, use hui-image-preview。 Standalone Vue imports @nscode/handy-uni/web HuiImagePreview or installs the preview plugin.
Examples
Interaction and event results
Interact with the control. The latest event and its full payload are shown below. Popup selectors commit only after confirmation; cancellation keeps the previous value.
<script setup lang="ts">
import { ref } from 'vue'
const result = ref('')
const visible = ref(false)
const urls = ['https://picsum.photos/600/800', 'https://picsum.photos/800/600']
function event0(arg0: unknown) {
result.value = 'update:visible' + ': ' + JSON.stringify([arg0])
}
function event1() {
result.value = 'open' + ': ' + JSON.stringify([])
}
function event2() {
result.value = 'opened' + ': ' + JSON.stringify([])
}
function event3(arg0: unknown) {
result.value = 'close' + ': ' + JSON.stringify([arg0])
}
function event4() {
result.value = 'closed' + ': ' + JSON.stringify([])
}
function event5(arg0: unknown) {
result.value = 'switch' + ': ' + JSON.stringify([arg0])
}
function event6(arg0: unknown) {
result.value = 'scale' + ': ' + JSON.stringify([arg0])
}
function event7(arg0: unknown) {
result.value = 'long-press' + ': ' + JSON.stringify([arg0])
}
</script>
<template>
<view>
<button @click="visible = true">打开预览 / Preview</button>
<hui-image-preview
v-model:visible="visible"
:url-list="urls"
closeable
@update:visible="event0"
@open="event1"
@opened="event2"
@close="event3"
@closed="event4"
@switch="event5"
@scale="event6"
@long-press="event7"
></hui-image-preview>
<text aria-live="polite">{{ result }}</text>
</view>
</template>
Behavior and platform support
showImagePreview({ urlList, initialIndex, native }) returns close(). Native preview does not support custom slots or close guards; use native:false and hui-page-host for these features. Custom preview mounts only the current and neighboring images.
Props
| Property | Type | Default | Description |
|---|---|---|---|
visible | boolean | undefined | undefined | Controlled popup visibility. |
default-visible | boolean | undefined | false | Initial uncontrolled visibility. |
url-list | string[] | undefined | [] | Preview image URLs. |
initial-index | number | undefined | 0 | Initial preview index, clamped to valid bounds. |
infinite | boolean | undefined | true | Loop between images. |
close-on-click-image | boolean | undefined | true | Close after a 300ms single-tap delay; double tap cancels closing and toggles zoom. |
show-index | boolean | undefined | true | Display and announce the current index and total. |
closeable | boolean | undefined | false | Show a close button. |
max-scale | number | undefined | 3 | Maximum image scale. |
min-scale | number | undefined | 1 | Minimum image scale. |
before-close | ((reason: ImagePreviewCloseReason) => boolean | void | Promise<boolean | void>) | undefined | — | Close guard; outside and exclusive closing bypass it. |
z-index | number | undefined | — | Explicit layer index; otherwise assigned by the page layer stack. |
root-portal | boolean | undefined | false | Use platform root-portal support on uni. |
custom-class | string | undefined | '' | Additional root classes. |
custom-style | string | CSSProperties | undefined | — | Additional root styles. |
Events
| Event | Parameters | Description |
|---|---|---|
update:visible | (visible: boolean) | Requests a controlled value update. |
open | () | Reports open with the arguments shown in the example. |
opened | () | Reports opened with the arguments shown in the example. |
close | (reason: ImagePreviewCloseReason) | Reports close with the arguments shown in the example. |
closed | () | Reports closed with the arguments shown in the example. |
switch | (index: number) | Reports switch with the arguments shown in the example. |
scale | (info: { index: number; scale: number; }) | Reports scale with the arguments shown in the example. |
long-press | (info: { index: number; url: string; }) | Reports long-press with the arguments shown in the example. |
Slots
| Slot | Description |
|---|---|
index | Index scope: {index,total}; index starts at zero. |
image | Image scope: {url,index}. |
cover | Overlay scope: {index}. |
Methods
| Method | Signature |
|---|---|
open | () => void |
close | (reason?: ImagePreviewCloseReason | undefined) => Promise<boolean> |
swipeTo | (index: number) => void |
Types
ImagePreviewProps、ImagePreviewEmits、ImagePreviewExpose。 Public types are exported from the package entry.
Platform notes
| Scope | Behavior and validation limits |
|---|---|
| Shared implementation | Uses a custom popup with platform swipe and zoom containers rather than the system image preview. |
| Device checks pending | Check swiping, pinch zoom, close callbacks and native stacking on WeChat and App; check touch zoom and scroll locking on Web. |
See examples/uni-demo/smoke/ImagePreview.md for the checklist. Unit tests do not replace device acceptance.