Skip to content
API 风格:

ImagePreview ​

A full-screen image preview component (a self-drawn form on web): the root node is a Teleport-ed shadowed overlay on body; the index is controlled through v-model (-1 / not passed = closed). It supports looped paging, an index indicator, wheel zoom, dragging to pan while zoomed in, horizontal swipe paging at the natural size and keyboard operation. Index math, zoom clamping, pan boundaries, text and accessibility labels are all computed by the pure logic in @lh-ui/share/core/image-preview (resolveImagePreviewIndex / clampScale / clampPan / getImagePreviewIndexText); this component only writes reactive glue and gesture bindings.

Boundary with the built-in viewer of the Image component: Image's preview + preview-src-list is a lightweight embedded "tap the image to view" form (toolbar buttons for zoom / rotation, no v-model index); ImagePreview is a standalone controlled preview layer that owns the image list and the current index itself, suited for list / masonry scenarios where "the images are not rendered on the page". The two do not depend on each other — pick either per scenario, or replace Image's preview entry with ImagePreview (taking over @click and images yourself).

Basic Usage ​

Two driving forms: bind the index with v-model (external state controls open/close), or grab a ref and call open() / close() (an internal state serves as fallback, also usable when unbound).

Controlled index
vue
<script setup lang="ts">
import { ref } from 'vue'
import { LhImagePreview } from '@lh-ui/vue3'

// -1 / not passed = closed; >= 0 is the current index
const index = ref(-1)
const images = [
  'https://picsum.photos/900/600?random=1',
  'https://picsum.photos/900/600?random=2',
  'https://picsum.photos/900/600?random=3',
]
</script>

<template>
  <LhButton size="small" @click="index = 0">Open the first image</LhButton>
  <LhImagePreview v-model="index" :images="images" />
</template>
Method calls
vue
<script setup lang="ts">
import { ref } from 'vue'
import { LhImagePreview } from '@lh-ui/vue3'

const preview = ref<InstanceType<typeof LhImagePreview>>()
const images = [
  'https://picsum.photos/900/600?random=1',
  'https://picsum.photos/900/600?random=2',
  'https://picsum.photos/900/600?random=3',
]
</script>

<template>
  <LhButton size="small" @click="preview?.open(0)">Method open(0)</LhButton>
  <LhImagePreview ref="preview" :images="images" />
</template>

Gestures and keyboard ​

  • Wheel zoom: scrolling the wheel over the image zooms directly (no Ctrl needed), within minZoom–maxZoom (1–3 by default), clamped by clampScale
  • Drag to pan: when scale > 1, press and drag to pan the image; the boundary is guarded by clampPan (no dark edges are exposed); on release it springs back over swipeDuration
  • Horizontal swipe paging: when scale == 1, a left/right swipe beyond 50px switches to the previous / next image; on narrow screens (≤768px) the desktop paging buttons hide automatically and gestures take over
  • Keyboard: while open, ← / → turn pages and Escape closes (window-level listener, operable even when focus is not inside the component)
  • Tap-to-close on the overlay can be turned off with closeOnPressOverlay
Loop and non-loop
vue
<LhImagePreview v-model="index" :images="images" :loop="false" :max-zoom="5" />

API ​

ImagePreview Props ​

PropDescriptionTypeDefault
imagesImage URL list (empty values are filtered out automatically)string[]—
modelValueCurrent index (controlled; -1 or not passed = closed)number—
loopLoop paging (true by default)booleantrue
showIndexShow the index indicator (true by default)booleantrue
swipeDurationSwipe animation duration in ms (300 by default)number300
closeOnPressOverlayClose on overlay tap (true by default)booleantrue
showCloseShow the close button on desktop (true by default)booleantrue
minZoomZoom lower bound (1 by default)number1
maxZoomZoom upper bound (3 by default)number3
labelAccessible name ("Image preview, image i of N" by default)string—

ImagePreview Events ​

EventDescriptionCallback
update:modelValueCurrent index write-back (including the closed state -1)(index: number)
changeFired when the user switches the index by interaction (open / previous / next / swipe / keyboard)(index: number)
closeFired on close—

ImagePreview Methods ​

The methods exposed by the component can be called via ref:

MethodDescriptionParameters
openOpen the preview (the index is normalized by core: loop wraps around with modulo, non-loop clamps to the boundary; 0 by default)(index?: number)
closeClose the preview (sets the index to -1 and emits close)—

Accessibility ​

  • The root node is role="dialog" + aria-modal="true"; its accessible name defaults to "Image preview, image i of N" and can be overridden with label
  • The close and paging buttons all carry aria-label (close / previous / next)
  • Keyboard operable: ← / → turn pages, Escape closes — no need to focus inside the component
  • No images are rendered while closed (v-if); no leftover media nodes remain in the DOM

Beauty of tradition, foundation of code