/*! For license information please see 6689.7ee8d445.js.LICENSE.txt */ (self.webpackChunk=self.webpackChunk||[]).push([["6689"],{20908:function(n,t,s){"use strict";s.r(t);var e=s("80681");let a=["innerHTML"];t.default={setup:()=>({html:""}),render:()=>((0,e.wg)(),(0,e.iD)("div",{class:"van-doc-markdown-body",innerHTML:'

ImagePreview

\n

Intro

\n

Used to zoom in and preview the picture, and it supports two methods: function call and component call.

\n

Install

\n

Register component globally via app.use, refer to Component Registration for more registration ways.

\n
import { createApp } from 'vue';\nimport { ImagePreview } from 'vant';\n\nconst app = createApp();\napp.use(ImagePreview);\n
\n

Function Call

\n

Vant provides some utility functions that can quickly evoke global ImagePreview components.

\n

For example, calling the showImagePreview function will render a Dialog directly in the page.

\n
import { showImagePreview } from 'vant';\n\nshowImagePreview(['https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg']);\n
\n

Usage

\n

Basic Usage

\n

When calling showImagePreview, you can directly pass an array of images to display the image preview.

\n
import { showImagePreview } from 'vant';\n\nshowImagePreview([\n  'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',\n  'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',\n]);\n
\n

Set Start Position

\n

showImagePreview supports passing a configuration object, and you can specify the initial position (index value) of the image through the startPosition option.

\n
import { showImagePreview } from 'vant';\n\nshowImagePreview({\n  images: [\n    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',\n    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',\n  ],\n  startPosition: 1,\n});\n
\n

Show Close Icon

\n

When the closeable option is enabled, a close icon will be displayed in the top-right corner of the popup layer. You can customize the icon by using the close-icon option, and the icon position can be customized using the close-icon-position option.

\n
import { showImagePreview } from 'vant';\n\nshowImagePreview({\n  images: [\n    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',\n    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',\n  ],\n  closeable: true,\n});\n
\n

Close Event

\n

You can listen to the close event of the image preview through the onClose option.

\n
import { showToast, showImagePreview } from 'vant';\n\nshowImagePreview({\n  images: [\n    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',\n    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',\n  ],\n  onClose() {\n    showToast('closed');\n  },\n});\n
\n

Before Close

\n

You can pass a callback function through the beforeClose option to perform specific operations before closing the image preview.

\n
import { showImagePreview } from 'vant';\n\nconst instance = showImagePreview({\n  images: [\n    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',\n    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',\n  ],\n  beforeClose: () => false,\n});\n\nsetTimeout(() => {\n  instance.close();\n}, 2000);\n
\n

Use ImagePreview Component

\n

If you need to embed components or other custom content inside the ImagePreview, you can directly use the ImagePreview component and customize it using the index slot. Before using it, you need to register the component through app.use or other methods.

\n
<van-image-preview v-model:show="show" :images="images" @change="onChange">\n  <template v-slot:index>Page: {{ index + 1 }}</template>\n</van-image-preview>\n
\n
import { ref } from 'vue';\n\nexport default {\n  setup() {\n    const show = ref(false);\n    const index = ref(0);\n    const images = [\n      'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',\n      'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',\n    ];\n    const onChange = (newIndex) => {\n      index.value = newIndex;\n    };\n\n    return {\n      show,\n      index,\n      images,\n      onChange,\n    };\n  },\n};\n
\n

Use image slot

\n

When using ImagePreview component, you can custom the image through the image slot, such as render a video content. In this example, you can also set close-on-click-image prop to false, so that the preview won\'t be accidentally closed when you click on the video.

\n
<van-image-preview\n  v-model:show="show"\n  :images="images"\n  :close-on-click-image="false"\n>\n  <template #image="{ src }">\n    <video style="width: 100%;" controls>\n      <source :src="src" />\n    </video>\n  </template>\n</van-image-preview>\n
\n
import { ref } from 'vue';\n\nexport default {\n  setup() {\n    const show = ref(false);\n    const images = [\n      'https://www.w3school.com.cn/i/movie.ogg',\n      'https://www.w3school.com.cn/i/movie.ogg',\n      'https://www.w3school.com.cn/i/movie.ogg',\n    ];\n    return {\n      show,\n      images,\n    };\n  },\n};\n
\n

When you customize the image through the image slot, you can bind the style and onLoad callback through the params of the slot, which can allow the <img> tag to support image scaling.

\n
<van-image-preview\n  v-model:show="show"\n  :images="images"\n  :close-on-click-image="false"\n>\n  <template #image="{ src, style, onLoad }">\n    <img :style="[{ width: '100%' }, style]" @load="onLoad" />\n  </template>\n</van-image-preview>\n
\n

API

\n

Methods

\n

Vant exports following ImagePreview utility functions:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
MethodsDescriptionAttributeReturn value
showImagePreviewDisplay a full-screen image preview componentstring[] | ImagePreviewOptionsImagePreview Instance
\n

ImagePreviewOptions

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeDescriptionTypeDefault
imagesImages URL liststring[][]
startPositionStart positionnumber | string0
showIndexWhether to show indexbooleantrue
showIndicatorsWhether to show indicatorsbooleanfalse
loopWhether to enable loopbooleantrue
swipeDurationAnimation duration (ms)number | string300
onCloseEmitted when ImagePreview is closedFunction-
onChangeEmitted when current image changedFunction-
onScaleEmitted when scaling current imageFunction-
closeOnPopstateWhether to close when popstatebooleantrue
doubleScale v4.7.2Whether to enable double tap zoom gesture. When disabled, the image preview will be closed immediately upon clickingbooleantrue
closeOnClickImage v4.8.3Whether to close when image is clickedbooleantrue
closeOnClickOverlay v4.6.4Whether to close when overlay is clickedbooleantrue
vertical v4.8.6Whether to enable vertical gesture slidingbooleanfalse
beforeCloseCallback function before close(action) => boolean | Promise-
classNameCustom classNamestring | Array | object-
maxZoomMax zoomnumber | string3
minZoomMin zoomnumber | string1/3
closeableWhether to show close iconbooleanfalse
closeIconClose icon namestringclear
closeIconPositionClose icon position, can be set to top-left bottom-left bottom-rightstringtop-right
transitionTransition, equivalent to name prop of transitionstringvan-fade
overlayClassCustom overlay classstring | Array | object-
overlayStyleCustom overlay styleobject-
teleportSpecifies a target element where ImagePreview will be mountedstring | Element-
\n

Props

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeDescriptionTypeDefault
v-model:showWhether to show ImagePreviewbooleanfalse
imagesImages URL liststring[][]
start-positionStart positionnumber | string0
swipe-durationAnimation duration (ms)number | string300
show-indexWhether to show indexbooleantrue
show-indicatorsWhether to show indicatorsbooleanfalse
loopWhether to enable loopbooleantrue
double-scaleWhether to enable double tap zoom gesture. When disabled, the image preview will be closed immediately upon clickingbooleantrue
before-closeCallback function before close(action: number) => boolean | Promise<boolean>-
close-on-popstateWhether to close when popstatebooleantrue
close-on-click-image v4.8.3Whether to close when image is clickedbooleantrue
close-on-click-overlay v4.6.4Whether to close when overlay is clickedbooleantrue
vertical v4.8.6Whether to enable vertical gesture slidingbooleanfalse
class-nameCustom className (apply to Popup in image preview)string | Array | object-
max-zoomMax zoomnumber | string3
min-zoomMin zoomnumber | string1/3
closeableWhether to show close iconbooleanfalse
close-iconClose icon namestringclear
close-icon-positionClose icon position, can be set to top-left bottom-left bottom-rightstringtop-right
transitionTransition, equivalent to name prop of transitionstringvan-fade
overlay-classCustom overlay classstring | Array | object-
overlay-styleCustom overlay styleobject-
teleportSpecifies a target element where ImagePreview will be mountedstring | Element-
\n

Events

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
EventDescriptionArguments
closeEmitted when closing ImagePreview{ index: number, url: string }
closedEmitted when ImagePreview is closed-
changeEmitted when current image changedindex: number
scaleEmitted when scaling current image{ index: number, scale: number }
long-pressEmitted when long press current image{ index: number }
\n

Methods

\n

Use ref to get ImagePreview instance and call instance methods.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDescriptionAttributeReturn value
resetScale 4.7.4Reset the current image\'s zoom ratio--
swipeToSwipe to target indexindex: number, options?: SwipeToOptions-
\n

Types

\n

The component exports the following type definitions:

\n
import type {\n  ImagePreviewProps,\n  ImagePreviewOptions,\n  ImagePreviewInstance,\n  ImagePreviewScaleEventParams,\n} from 'vant';\n
\n

ImagePreviewInstance is the type of component instance:

\n
import { ref } from 'vue';\nimport type { ImagePreviewInstance } from 'vant';\n\nconst imagePreviewRef = ref<ImagePreviewInstance>();\n\nimagePreviewRef.value?.swipeTo(1);\n
\n

Slots

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDescriptionSlotProps
indexCustom index{ index: index of current image }
coverCustom content that covers the image preview-
imageCustom image content{ src: current image src, onLoad: load image, style: current image style }
\n

onClose Parameters

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeDescriptionType
urlURL of current imagenumber
indexIndex of current imagenumber
\n

onScale Parameters

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeDescriptionType
indexIndex of current imagenumber
scalescale of current imagenumber
\n

Theming

\n

CSS Variables

\n

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDefault ValueDescription
--van-image-preview-index-text-colorvar(--van-white)-
--van-image-preview-index-font-sizevar(--van-font-size-md)-
--van-image-preview-index-line-heightvar(--van-line-height-md)-
--van-image-preview-index-text-shadow0 1px 1px var(--van-gray-8)-
--van-image-preview-overlay-backgroundrgba(0, 0, 0, 0.9)-
--van-image-preview-close-icon-size22px-
--van-image-preview-close-icon-colorvar(--van-gray-5)-
--van-image-preview-close-icon-marginvar(--van-padding-md)-
--van-image-preview-close-icon-z-index1-
\n
'},null,8,a))}}}]);