# 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 00:49:57

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