ImageViewer 图片预览器
基础用法
使用urlList
传入需要预览的图片列表,使用initialIndex
指定预览的初始索引。使用实例的open
方法来打开预览框,使用实例的close
方法来关闭预览框。
TIP
注意: 底层使用了Vue3
的Teleport
组件,具体用法可参考Teleport
组件文档。
<template>
<div class="container">
<p>
<ym-button @click="open" type="primary">打开</ym-button>
</p>
<ym-image-viewer ref="imageViewerRef" :urlList="urlList" :initialIndex="0"></ym-image-viewer>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ImageViewerExpose } from 'guyan-ym-ui'
const urlList = [
"https://haowallpaper.com/link/common/file/getCroppingImg/15946441491795328",
"https://haowallpaper.com/link/common/file/getCroppingImg/15918089447001472",
"https://haowallpaper.com/link/common/file/getCroppingImg/16026675077107072",
"https://haowallpaper.com/link/common/file/getCroppingImg/15701594680365376",
"https://haowallpaper.com/link/common/file/getCroppingImg/15946470577982848",
"https://haowallpaper.com/link/common/file/getCroppingImg/15947843037547904"
]
const imageViewerRef = ref<ImageViewerExpose>()
const open = () => {
imageViewerRef.value?.open()
}
</script>
<style scoped>
</style>
关闭预览器
可以点击预览器右上角的关闭图标来关闭预览器,也可以使用实例的close
方法来关闭预览器。 可以使用 hideOnClickModal
属性来指定是否点击遮罩层关闭预览器。 可以使用closeOnPressEsc
属性来指定是否按下ESC键关闭预览器。
<template>
<div class="container">
<p>
<ym-button @click="open" type="primary">打开</ym-button>
</p>
<ym-image-viewer
ref="imageViewerRef"
:urlList="urlList"
:initialIndex="0"
hideOnClickModal
closeOnPressEsc
></ym-image-viewer>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ImageViewerExpose } from 'guyan-ym-ui'
const urlList = [
"https://haowallpaper.com/link/common/file/getCroppingImg/15946441491795328",
"https://haowallpaper.com/link/common/file/getCroppingImg/15918089447001472",
"https://haowallpaper.com/link/common/file/getCroppingImg/16026675077107072",
"https://haowallpaper.com/link/common/file/getCroppingImg/15701594680365376",
"https://haowallpaper.com/link/common/file/getCroppingImg/15946470577982848",
"https://haowallpaper.com/link/common/file/getCroppingImg/15947843037547904"
]
const imageViewerRef = ref<ImageViewerExpose>()
const open = () => {
imageViewerRef.value?.open()
}
</script>
<style scoped>
</style>
缩放旋转
可以使用zoomRate
来指定图片的缩放比例,使用minScale
来指定图片的最小缩放比例,使用maxScale
来指定图片的最大缩放比例。 打开预览器后可以使用滚轮或者底部放大缩小按钮来缩放图片。可以使用底部旋转按钮来旋转图片。
<template>
<div class="container">
<p>
<ym-button @click="open" type="primary">打开</ym-button>
</p>
<ym-image-viewer
ref="imageViewerRef"
:urlList="urlList"
:initialIndex="0"
:zoomRate="0.3"
:minScale="0.1"
:maxScale="10"
></ym-image-viewer>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ImageViewerExpose } from 'guyan-ym-ui'
const urlList = [
"https://haowallpaper.com/link/common/file/getCroppingImg/15946441491795328",
"https://haowallpaper.com/link/common/file/getCroppingImg/15918089447001472",
"https://haowallpaper.com/link/common/file/getCroppingImg/16026675077107072",
"https://haowallpaper.com/link/common/file/getCroppingImg/15701594680365376",
"https://haowallpaper.com/link/common/file/getCroppingImg/15946470577982848",
"https://haowallpaper.com/link/common/file/getCroppingImg/15947843037547904"
]
const imageViewerRef = ref<ImageViewerExpose>()
const open = () => {
imageViewerRef.value?.open()
}
</script>
<style scoped>
</style>
ImageViewer API
Props
Name | Description | Type | Default |
---|---|---|---|
urlList | 图片列表 | string[] | [] |
initialIndex | 初始索引 | number | 0 |
hideOnClickModal | 是否点击遮罩层关闭预览器 | boolean | true |
closeOnPressEsc | 是否按下ESC键关闭预览器 | boolean | true |
zoomRate | 图片缩放比例 | number | 1 |
minScale | 图片最小缩放比例 | number | 0.1 |
maxScale | 图片最大缩放比例 | number | 5 |
Events
Name | Description | Type |
---|---|---|
close | 关闭预览器时触发 | () => void |
switch | 切换图片时触发 | (url: string, index: number) => void |
rotate | 旋转图片时触发 | (angle: number) => void |
wheel | 滚轮事件 | (event: WheelEvent) => void |