Skip to content
On this page

ImagePreview 图片预览

vue
<template>
  <ta-group title="基础用法">
    <ta-cell label="预览图片" isLink @click="show({})" />
    <ta-cell
      label="指定初始图片"
      isLink
      @click="show({ current: 'https://cdn.fox2.cn/vfox/swiper/different-2.jpg' })"
    />
    <ta-cell label="展示关闭按钮" isLink @click="show({ showClose: true })" />
  </ta-group>
  <ta-group title="事件监听">
    <ta-cell label="change/cancel" isLink @click="show({ changeEvent: true })" />
    <ta-cell label="visible-state-change" isLink @click="show({ visibleEvent: true })" />
  </ta-group>
  <ta-group title="API">
    <ta-cell label="showImagePreview" isLink @click="onCallApi" />
  </ta-group>
  <ta-image-preview
    v-model:visible="visible"
    :urls="imageUrls"
    v-model="current"
    :showClose="showClose"
    :imageHighRendering="false"
    :magnification="1.2"
    @visibleStateChange="onVisibleStateChange"
    @cancel="onCancel"
    @change="onChange"
  />
</template>

<script lang="ts">
import {
  type ImagePreviewOnChange,
  type PopupOnCancel,
  type PopupOnVisibleStateChange,
  showImagePreview,
  showToast
} from '@/index'
import { defineComponent, ref } from 'vue'

interface showArgs {
  showClose?: boolean
  current?: string
  changeEvent?: boolean
  visibleEvent?: boolean
}

export default defineComponent({
  name: 'ExpImagePreview',
  setup() {
    const visible = ref(false)
    const showClose = ref(false)
    const changeEvent = ref(false)
    const visibleEvent = ref(false)

    const current = ref('')

    const imageUrls = [
      'https://cdn.fox2.cn/vfox/swiper/different-1.jpg',
      'https://cdn.fox2.cn/vfox/swiper/different-2.jpg',
      'https://cdn.fox2.cn/vfox/swiper/different-3.jpg'
    ]

    function onCallApi() {
      showImagePreview({
        value: imageUrls[1],
        urls: imageUrls,
        showClose: true,
        imageHighRendering: false,
        success: res => {
          console.log('success', res)
        }
      })
    }

    function show(res: showArgs) {
      showClose.value = res.showClose || false
      current.value = res.current || ''
      changeEvent.value = res.changeEvent || false
      visibleEvent.value = res.visibleEvent || false
      visible.value = true
    }

    const onVisibleStateChange: PopupOnVisibleStateChange = res => {
      if (visibleEvent.value) {
        console.log('visible-state-change', res)
        showToast(`${res.state} 事件触发`)
      }
      if (res.state === 'hidden') {
        showClose.value = false
        current.value = ''
        changeEvent.value = false
        visibleEvent.value = false
      }
    }

    const onChange: ImagePreviewOnChange = (current, activeIndex) => {
      if (changeEvent.value) {
        console.log('change', { current, activeIndex })
        showToast(`切换到第 ${activeIndex + 1}`)
      }
    }

    const onCancel: PopupOnCancel = res => {
      if (changeEvent.value) {
        console.log('cancel', res)
        showToast(`关闭`)
      }
    }

    return {
      visible,
      showClose,

      imageUrls,
      current,

      onCallApi,
      show,
      onVisibleStateChange,
      onChange,
      onCancel
    }
  }
})
</script>

Import

js
import { TaImagePreview } from 'tantalum-ui-mobile'

具体的引入方式可以参考引入组件

Import Type

组件导出的类型定义:

ts
import type {
  ImagePreviewOnChange,
  VisibleState,
  PopupOnVisibleStateChange,
  PopupOnCancel
} from 'tantalum-ui-mobile'

Props

属性类型默认值必填说明
v-model:visiblebooleanfalse是否显示
v-modelstring指定当前显示的图片 url
urlsstring[]图片 url 数组
show-closebooleanfalse是否显示关闭按钮,显示按钮后展示头部栏
navigation-buttonsbooleanfalse是否展示上一页/下一页按钮
image-high-renderingbooleantrue高清渲染,开启后图片按物理分辨率展示
magnificationstring | number1如果图片本身不够高清,大于 1 放大后会产生模糊

Events

事件描述回调函数参数TypeScript 函数
change图片切换后触发( current: string, activeIndex: number, fromIndex: number )ImagePreviewOnChange
cancel关闭时触发( payload: { source: string } )PopupOnCancel
visible-state-change展示隐藏时触发( payload: { state: VisibleState } )PopupOnVisibleStateChange

change 的回调参数

参数类型描述
currentstring当前图片的 url
activeIndexnumber当前图片的索引
fromIndexnumber上涨图片的索引

VisibleState 值说明

说明备注
show展示时触发
shown展示且动画结束后触发
hide隐藏时触发可能携带其他参数 cancel, maskClick, closeClick 等
hidden隐藏且动画结束后触发可能携带其他参数 cancel, maskClick, closeClick 等

showImagePreview(object)

预览图片。

object

属性类型默认值必填说明
urlsstring[]图片地址数组
valuestring默认显示的图片地址
showClosebooleanfalse是否显示关闭按钮,显示按钮后展示头部栏
navigationButtonsbooleanfalse是否展示上一页/下一页按钮
imageHighRenderingbooleantrue高清渲染,开启后图片按物理分辨率展示
magnificationstring | number1如果图片本身不够高清,大于 1 放大后会产生模糊
success(payload: { cancel: boolean }) => void接口调用成功(在用户做出选择后,如取消,选择选项)的回调函数
fail(e: Error) => void接口调用失败(如传入错误的参数)的回调函数(不传入 fail 遇错误直接抛出)
complete() => void弹窗关闭或调用失败的回调函数

Usage

具体调用方式可以参考API 调用

js
showImagePreview({
  urls: [
    'https://cdn.fox2.cn/vfox/empty/default@2x.png',
    'https://cdn.fox2.cn/vfox/empty/network@2x.png'
  ],
  value: 'https://cdn.fox2.cn/vfox/empty/network@2x.png'
})