# ImagePreview 圖片預覽

圖片預覽彈層:全螢幕遮罩 + 圖片瀏覽,支援下標指示、桌面端翻頁 / 關閉按鈕、 鍵盤 ←/→ 翻頁與 Esc 關閉、滾輪縮放、拖曳平移與水平滑動翻頁。下標換算、 縮放鉗制與拖曳邊界全部在 @lh-ui/share/core/image-preview 純邏輯裡, 本元件只做響應式膠水與手勢模板;樣式來自 share styles/image-preview。

modelValue 為受控當前下標(-1 或不傳 = 關閉)。雙綁用 .sync 約定:

<LhImagePreview :images="images" :model-value.sync="current" />
圖片預覽(受控開啟 / loop / 下標指示 / 遮罩關閉)
<template>
  <div class="demo-col" style="gap: 18px; max-width: 480px">
    <div class="demo-row" style="gap: 8px">
      <LhButton size="small" @click="open(0)">開啟第 1 張</LhButton>
      <LhButton size="small" @click="open(2)">開啟第 3 張</LhButton>
      <LhButton size="small" @click="open(99)">越界下標歸一</LhButton>
    </div>
    <div class="demo-row" style="gap: 8px">
      <LhButton size="small" @click="loop = !loop">loop 切換(當前 {{ loop }})</LhButton>
      <LhButton size="small" @click="showIndex = !showIndex">下標指示({{ showIndex }})</LhButton>
      <LhButton size="small" @click="closeOverlay = !closeOverlay">點遮罩關閉({{ closeOverlay }})</LhButton>
    </div>
    <LhImagePreview
      :images="images"
      :model-value.sync="current"
      :loop="loop"
      :show-index="showIndex"
      :close-on-press-overlay="closeOverlay"
      label="水墨畫冊"
    />
    <span class="demo-note">current = {{ current }}(-1 = 關閉);←/→ 翻頁、Esc 關閉、滾輪縮放</span>
  </div>
</template>

<script>
export default {
  data: () => ({
    current: -1,
    loop: true,
    showIndex: true,
    closeOverlay: true,
    images: [
      'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27640%27%20height=%27420%27%3E%3Crect%20width=%27640%27%20height=%27420%27%20fill=%27%23e8e2d5%27/%3E%3Ctext%20x=%2750%%27%20y=%2750%%27%20font-size=%2740%27%20fill=%27%232b4c7c%27%20text-anchor=%27middle%27%20font-family=%27serif%27%3E%E6%BA%AA%E5%B1%B1%3C/text%3E%3C/svg%3E',
      'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27640%27%20height=%27420%27%3E%3Crect%20width=%27640%27%20height=%27420%27%20fill=%27%23d8d0bd%27/%3E%3Ctext%20x=%2750%%27%20y=%2750%%27%20font-size=%2740%27%20fill=%27%23a63d40%27%20text-anchor=%27middle%27%20font-family=%27serif%27%3E%E6%99%B4%E5%B2%9A%3C/text%3E%3C/svg%3E',
      'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27640%27%20height=%27420%27%3E%3Crect%20width=%27640%27%20height=%27420%27%20fill=%27%23cfc6b0%27/%3E%3Ctext%20x=%2750%%27%20y=%2750%%27%20font-size=%2740%27%20fill=%27%233a7d5a%27%20text-anchor=%27middle%27%20font-family=%27serif%27%3E%E6%9D%BE%E9%A3%8E%3C/text%3E%3C/svg%3E',
    ],
  }),
  methods: {
    open(index) {
      this.current = index
    },
  },
}
</script>

# 受控下標

modelValue 是受控下標:-1(或不傳)為關閉態,>= 0 為開啟態。 翻頁 / 滑動 / open() 都通過 update:modelValue 回流歸一後的下標, 外部直接修改 modelValue 同樣生效(越界下標經 resolveImagePreviewIndex 歸一:loop 時取模回繞,非 loop 時鉗制到邊界):

<LhImagePreview :images="images" :model-value="2" />

# 翻頁與循環

loop(預設 true)開啟時首尾回繞;關閉時邊界處隱藏桌面翻頁按鈕 (getPrevIndex / getNextIndex 傳回 -1 即不渲染)。行動端(窄螢幕) 翻頁按鈕由樣式自動隱藏,手勢翻頁接管:

<LhImagePreview :images="images" :model-value.sync="current" :loop="false" />

# 手勢與鍵盤

  • 水平滑動(原尺寸態,位移超 50px):右滑上一張、左滑下一張
  • 拖曳平移(縮放態 scale > 1):clampPan 鉗制不露邊緣
  • 滾輪縮放:按檔位進退,鉗制到 [minZoom, maxZoom](預設 1~3)
  • 鍵盤:← / → 翻頁,Esc 關閉(可見時生效)
  • 點選遮罩:closeOnPressOverlay(預設 true)時關閉

swipeDuration(預設 300ms)控制翻頁 / 回彈的過渡時長。

# 方法

經 ref 調用(defineExpose 暴露):

方法 說明
open(index?) 開啟預覽(預設第 0 張;下標經歸一)
close() 關閉預覽(置 -1 並拋 close)
prev() 上一張(邊界且非循環時無操作)
next() 下一張(邊界且非循環時無操作)
<LhImagePreview ref="preview" :images="images" :model-value.sync="current" />
this.$refs.preview.open(1)

# Props

屬性 說明 類型 預設值
images 圖片位址列表(空 url 自動過濾) string[] []
modelValue 當前下標(受控;-1 / 不傳 = 關閉) number -
loop 循環翻頁 boolean true
showIndex 展示下標指示(如「1 / 5」) boolean true
swipeDuration 滑動 / 翻頁過渡時長 ms number 300
closeOnPressOverlay 點選遮罩關閉 boolean true
showClose 桌面端展示關閉按鈕 boolean true
minZoom 縮放下限 number 1
maxZoom 縮放上限 number 3
label 無障礙可達名稱(未傳走預設文案) string —

# Events

事件 說明 回呼參數
update:modelValue 下標變化(.sync 回流,含關閉時的 -1) value: number
change 預覽下標切換 index: number
close 預覽關閉 —

# 無障礙

根元素由 getImagePreviewAria 注入 aria-label(自訂 label 優先, 否則播報「圖片預覽 第 i / N 張」),並宣告 role="dialog"、 aria-modal="true";翻頁 / 關閉按鈕均帶 aria-label。

最後更新: 2026/12/4 上午12:49:57

Copyright © 2024-2026 风默文学 | 宁ICP备2024006574号 | 公安备案徽标苏公网安备32040002010537