ImagePreview 图片预览
WebH5
图片预览。全屏遮罩查看图片,支持多图切换、滚轮缩放(0.5x-3x)、双击切换 1x/2x、Esc 关闭;可组件式使用也可 showImagePreview 服务式调起。
何时使用
点击图片后需要全屏查看、缩放或在多图间切换。
何时不使用
单张图简单放大可用 Image 的 preview;图文混排详情用 Dialog。
Props
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
modelValue | boolean | false | 显隐(v-model) |
images | string[] | "-" | 图片地址列表(必填) |
startIndex | number | 0 | 初始显示第几张(从 0 开始) |
Events
| 名称 | 载荷 | 说明 |
|---|---|---|
update:modelValue | boolean | 显隐变化时触发 |
close | - | 关闭时触发(遮罩点击 / × / Esc) |
change | index: number | 切换图片时触发,参数为新索引 |
示例
组件式(多图切换)
示例加载中…
<div>
<Button variant="primary" @click="ipv1 = true">查看产品图(3 张)</Button>
</div>
<ImagePreview
v-model="ipv1"
:images="[
'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27400%27%20height=%27300%27%3E%3Cdefs%3E%3ClinearGradient%20id=%27g%27%20x1=%270%27%20y1=%270%27%20x2=%271%27%20y2=%271%27%3E%3Cstop%20offset=%270%27%20stop-color=%27%23667eea%27/%3E%3Cstop%20offset=%271%27%20stop-color=%27%23764ba2%27/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width=%27400%27%20height=%27300%27%20fill=%27url(%23g)%27/%3E%3C/svg%3E',
'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27400%27%20height=%27300%27%3E%3Cdefs%3E%3ClinearGradient%20id=%27g%27%20x1=%270%27%20y1=%270%27%20x2=%271%27%20y2=%271%27%3E%3Cstop%20offset=%270%27%20stop-color=%27%23f6d365%27/%3E%3Cstop%20offset=%271%27%20stop-color=%27%23fda085%27/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width=%27400%27%20height=%27300%27%20fill=%27url(%23g)%27/%3E%3C/svg%3E',
'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27400%27%20height=%27300%27%3E%3Cdefs%3E%3ClinearGradient%20id=%27g%27%20x1=%270%27%20y1=%270%27%20x2=%271%27%20y2=%271%27%3E%3Cstop%20offset=%270%27%20stop-color=%27%2384fab0%27/%3E%3Cstop%20offset=%271%27%20stop-color=%27%238fd3f4%27/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width=%27400%27%20height=%27300%27%20fill=%27url(%23g)%27/%3E%3C/svg%3E',
]"
/>配合 Image 组件
示例加载中…
<Image src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='300'%3E%3Cdefs%3E%3ClinearGradient%20id='g'%20x1='0'%20y1='0'%20x2='1'%20y2='1'%3E%3Cstop%20offset='0'%20stop-color='%2384fab0'/%3E%3Cstop%20offset='1'%20stop-color='%238fd3f4'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width='400'%20height='300'%20fill='url(%23g)'/%3E%3C/svg%3E" alt="点击预览" preview width="200" height="120" radius />单图预览
示例加载中…
<div>
<Button variant="secondary" @click="ipv2 = true">查看大图</Button>
</div>
<ImagePreview
v-model="ipv2"
:images="['data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27400%27%20height=%27300%27%3E%3Cdefs%3E%3ClinearGradient%20id=%27g%27%20x1=%270%27%20y1=%270%27%20x2=%271%27%20y2=%271%27%3E%3Cstop%20offset=%270%27%20stop-color=%27%23667eea%27/%3E%3Cstop%20offset=%271%27%20stop-color=%27%23764ba2%27/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width=%27400%27%20height=%27300%27%20fill=%27url(%23g)%27/%3E%3C/svg%3E']"
/>指定初始索引
示例加载中…
<div>
<Button @click="ipv3 = true">从第 2 张开始查看</Button>
</div>
<ImagePreview
v-model="ipv3"
:start-index="1"
:images="[
'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27400%27%20height=%27300%27%3E%3Cdefs%3E%3ClinearGradient%20id=%27g%27%20x1=%270%27%20y1=%270%27%20x2=%271%27%20y2=%271%27%3E%3Cstop%20offset=%270%27%20stop-color=%27%23f6d365%27/%3E%3Cstop%20offset=%271%27%20stop-color=%27%23fda085%27/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width=%27400%27%20height=%27300%27%20fill=%27url(%23g)%27/%3E%3C/svg%3E',
'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27400%27%20height=%27300%27%3E%3Cdefs%3E%3ClinearGradient%20id=%27g%27%20x1=%270%27%20y1=%270%27%20x2=%271%27%20y2=%271%27%3E%3Cstop%20offset=%270%27%20stop-color=%27%2384fab0%27/%3E%3Cstop%20offset=%271%27%20stop-color=%27%238fd3f4%27/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width=%27400%27%20height=%27300%27%20fill=%27url(%23g)%27/%3E%3C/svg%3E',
'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27400%27%20height=%27300%27%3E%3Cdefs%3E%3ClinearGradient%20id=%27g%27%20x1=%270%27%20y1=%270%27%20x2=%271%27%20y2=%271%27%3E%3Cstop%20offset=%270%27%20stop-color=%27%23667eea%27/%3E%3Cstop%20offset=%271%27%20stop-color=%27%23764ba2%27/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width=%27400%27%20height=%27300%27%20fill=%27url(%23g)%27/%3E%3C/svg%3E',
]"
/>事件监听
示例加载中…
<div>
<Button variant="ghost" @click="ipv4 = true">预览并监听事件</Button>
</div>
<ImagePreview
v-model="ipv4"
@change="console.log('切换到第', $event, '张')"
@close="console.log('预览关闭')"
:images="[
'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27400%27%20height=%27300%27%3E%3Cdefs%3E%3ClinearGradient%20id=%27g%27%20x1=%270%27%20y1=%270%27%20x2=%271%27%20y2=%271%27%3E%3Cstop%20offset=%270%27%20stop-color=%27%2384fab0%27/%3E%3Cstop%20offset=%271%27%20stop-color=%27%238fd3f4%27/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width=%27400%27%20height=%27300%27%20fill=%27url(%23g)%27/%3E%3C/svg%3E',
'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27400%27%20height=%27300%27%3E%3Cdefs%3E%3ClinearGradient%20id=%27g%27%20x1=%270%27%20y1=%270%27%20x2=%271%27%20y2=%271%27%3E%3Cstop%20offset=%270%27%20stop-color=%27%23f6d365%27/%3E%3Cstop%20offset=%271%27%20stop-color=%27%23fda085%27/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width=%27400%27%20height=%27300%27%20fill=%27url(%23g)%27/%3E%3C/svg%3E',
]"
/>业务场景:商品详情图集
示例加载中…
<Stack :gap="12">
<div style="display:flex;gap:8px;">
<Image src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='300'%3E%3Cdefs%3E%3ClinearGradient%20id='g'%20x1='0'%20y1='0'%20x2='1'%20y2='1'%3E%3Cstop%20offset='0'%20stop-color='%23667eea'/%3E%3Cstop%20offset='1'%20stop-color='%23764ba2'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width='400'%20height='300'%20fill='url(%23g)'/%3E%3C/svg%3E" alt="封面" width="120" height="80" radius />
<Image src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='300'%3E%3Cdefs%3E%3ClinearGradient%20id='g'%20x1='0'%20y1='0'%20x2='1'%20y2='1'%3E%3Cstop%20offset='0'%20stop-color='%23f6d365'/%3E%3Cstop%20offset='1'%20stop-color='%23fda085'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width='400'%20height='300'%20fill='url(%23g)'/%3E%3C/svg%3E" alt="细节1" width="120" height="80" radius />
<Image src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='300'%3E%3Cdefs%3E%3ClinearGradient%20id='g'%20x1='0'%20y1='0'%20x2='1'%20y2='1'%3E%3Cstop%20offset='0'%20stop-color='%2384fab0'/%3E%3Cstop%20offset='1'%20stop-color='%238fd3f4'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect%20width='400'%20height='300'%20fill='url(%23g)'/%3E%3C/svg%3E" alt="细节2" width="120" height="80" radius />
</div>
<p style="font-size:12px;color:var(--rn-color-text-placeholder)">点击缩略图或预览按钮查看大图</p>
</Stack>Playground
加载中…
跨端版本
该组件同时提供 uni-app / uni-app X 实现(H5 / 微信小程序 / App),API 可能略有差异,见 uni-app 端 ImagePreview 文档(含手机预览)。