Skip to content

ImageViewer 图片预览器

基础用法

使用urlList传入需要预览的图片列表,使用initialIndex指定预览的初始索引。使用实例的open方法来打开预览框,使用实例的close方法来关闭预览框。

TIP

注意: 底层使用了Vue3Teleport组件,具体用法可参考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

NameDescriptionTypeDefault
urlList图片列表string[][]
initialIndex初始索引number0
hideOnClickModal是否点击遮罩层关闭预览器booleantrue
closeOnPressEsc是否按下ESC键关闭预览器booleantrue
zoomRate图片缩放比例number1
minScale图片最小缩放比例number0.1
maxScale图片最大缩放比例number5

Events

NameDescriptionType
close关闭预览器时触发() => void
switch切换图片时触发(url: string, index: number) => void
rotate旋转图片时触发(angle: number) => void
wheel滚轮事件(event: WheelEvent) => void