/*! 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:'
Used to zoom in and preview the picture, and it supports two methods: function call and component call.
\nRegister component globally via app.use
, refer to Component Registration for more registration ways.
import { createApp } from 'vue';\nimport { ImagePreview } from 'vant';\n\nconst app = createApp();\napp.use(ImagePreview);\n
\nVant provides some utility functions that can quickly evoke global ImagePreview
components.
For example, calling the showImagePreview
function will render a Dialog directly in the page.
import { showImagePreview } from 'vant';\n\nshowImagePreview(['https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg']);\n
\nWhen calling showImagePreview
, you can directly pass an array of images to display the image preview.
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
\nshowImagePreview
supports passing a configuration object, and you can specify the initial position (index value) of the image through the startPosition
option.
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
\nWhen 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.
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
\nYou can listen to the close event of the image preview through the onClose
option.
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
\nYou can pass a callback function through the beforeClose
option to perform specific operations before closing the image preview.
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
\nIf 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.
<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
\nimport { 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
\nWhen 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.
<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
\nimport { 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
\nWhen 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.
<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
\nVant exports following ImagePreview utility functions:
\nMethods | \nDescription | \nAttribute | \nReturn value | \n
---|---|---|---|
showImagePreview | \nDisplay a full-screen image preview component | \nstring[] | ImagePreviewOptions | \nImagePreview Instance | \n
Attribute | \nDescription | \nType | \nDefault | \n
---|---|---|---|
images | \nImages URL list | \nstring[] | \n[] | \n
startPosition | \nStart position | \nnumber | string | \n0 | \n
showIndex | \nWhether to show index | \nboolean | \ntrue | \n
showIndicators | \nWhether to show indicators | \nboolean | \nfalse | \n
loop | \nWhether to enable loop | \nboolean | \ntrue | \n
swipeDuration | \nAnimation duration (ms) | \nnumber | string | \n300 | \n
onClose | \nEmitted when ImagePreview is closed | \nFunction | \n- | \n
onChange | \nEmitted when current image changed | \nFunction | \n- | \n
onScale | \nEmitted when scaling current image | \nFunction | \n- | \n
closeOnPopstate | \nWhether to close when popstate | \nboolean | \ntrue | \n
doubleScale v4.7.2 | \nWhether to enable double tap zoom gesture. When disabled, the image preview will be closed immediately upon clicking | \nboolean | \ntrue | \n
closeOnClickImage v4.8.3 | \nWhether to close when image is clicked | \nboolean | \ntrue | \n
closeOnClickOverlay v4.6.4 | \nWhether to close when overlay is clicked | \nboolean | \ntrue | \n
vertical v4.8.6 | \nWhether to enable vertical gesture sliding | \nboolean | \nfalse | \n
beforeClose | \nCallback function before close | \n(action) => boolean | Promise | \n- | \n
className | \nCustom className | \nstring | Array | object | \n- | \n
maxZoom | \nMax zoom | \nnumber | string | \n3 | \n
minZoom | \nMin zoom | \nnumber | string | \n1/3 | \n
closeable | \nWhether to show close icon | \nboolean | \nfalse | \n
closeIcon | \nClose icon name | \nstring | \nclear | \n
closeIconPosition | \nClose icon position, can be set to top-left bottom-left bottom-right | \nstring | \ntop-right | \n
transition | \nTransition, equivalent to name prop of transition | \nstring | \nvan-fade | \n
overlayClass | \nCustom overlay class | \nstring | Array | object | \n- | \n
overlayStyle | \nCustom overlay style | \nobject | \n- | \n
teleport | \nSpecifies a target element where ImagePreview will be mounted | \nstring | Element | \n- | \n
Attribute | \nDescription | \nType | \nDefault | \n
---|---|---|---|
v-model:show | \nWhether to show ImagePreview | \nboolean | \nfalse | \n
images | \nImages URL list | \nstring[] | \n[] | \n
start-position | \nStart position | \nnumber | string | \n0 | \n
swipe-duration | \nAnimation duration (ms) | \nnumber | string | \n300 | \n
show-index | \nWhether to show index | \nboolean | \ntrue | \n
show-indicators | \nWhether to show indicators | \nboolean | \nfalse | \n
loop | \nWhether to enable loop | \nboolean | \ntrue | \n
double-scale | \nWhether to enable double tap zoom gesture. When disabled, the image preview will be closed immediately upon clicking | \nboolean | \ntrue | \n
before-close | \nCallback function before close | \n(action: number) => boolean | Promise<boolean> | \n- | \n
close-on-popstate | \nWhether to close when popstate | \nboolean | \ntrue | \n
close-on-click-image v4.8.3 | \nWhether to close when image is clicked | \nboolean | \ntrue | \n
close-on-click-overlay v4.6.4 | \nWhether to close when overlay is clicked | \nboolean | \ntrue | \n
vertical v4.8.6 | \nWhether to enable vertical gesture sliding | \nboolean | \nfalse | \n
class-name | \nCustom className (apply to Popup in image preview) | \nstring | Array | object | \n- | \n
max-zoom | \nMax zoom | \nnumber | string | \n3 | \n
min-zoom | \nMin zoom | \nnumber | string | \n1/3 | \n
closeable | \nWhether to show close icon | \nboolean | \nfalse | \n
close-icon | \nClose icon name | \nstring | \nclear | \n
close-icon-position | \nClose icon position, can be set to top-left bottom-left bottom-right | \nstring | \ntop-right | \n
transition | \nTransition, equivalent to name prop of transition | \nstring | \nvan-fade | \n
overlay-class | \nCustom overlay class | \nstring | Array | object | \n- | \n
overlay-style | \nCustom overlay style | \nobject | \n- | \n
teleport | \nSpecifies a target element where ImagePreview will be mounted | \nstring | Element | \n- | \n
Event | \nDescription | \nArguments | \n
---|---|---|
close | \nEmitted when closing ImagePreview | \n{ index: number, url: string } | \n
closed | \nEmitted when ImagePreview is closed | \n- | \n
change | \nEmitted when current image changed | \nindex: number | \n
scale | \nEmitted when scaling current image | \n{ index: number, scale: number } | \n
long-press | \nEmitted when long press current image | \n{ index: number } | \n
Use ref to get ImagePreview instance and call instance methods.
\nName | \nDescription | \nAttribute | \nReturn value | \n
---|---|---|---|
resetScale 4.7.4 | \nReset the current image\'s zoom ratio | \n- | \n- | \n
swipeTo | \nSwipe to target index | \nindex: number, options?: SwipeToOptions | \n- | \n
The component exports the following type definitions:
\nimport type {\n ImagePreviewProps,\n ImagePreviewOptions,\n ImagePreviewInstance,\n ImagePreviewScaleEventParams,\n} from 'vant';\n
\nImagePreviewInstance
is the type of component instance:
import { ref } from 'vue';\nimport type { ImagePreviewInstance } from 'vant';\n\nconst imagePreviewRef = ref<ImagePreviewInstance>();\n\nimagePreviewRef.value?.swipeTo(1);\n
\nName | \nDescription | \nSlotProps | \n
---|---|---|
index | \nCustom index | \n{ index: index of current image } | \n
cover | \nCustom content that covers the image preview | \n- | \n
image | \nCustom image content | \n{ src: current image src, onLoad: load image, style: current image style } | \n
Attribute | \nDescription | \nType | \n
---|---|---|
url | \nURL of current image | \nnumber | \n
index | \nIndex of current image | \nnumber | \n
Attribute | \nDescription | \nType | \n
---|---|---|
index | \nIndex of current image | \nnumber | \n
scale | \nscale of current image | \nnumber | \n
The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.
\nName | \nDefault Value | \nDescription | \n
---|---|---|
--van-image-preview-index-text-color | \nvar(--van-white) | \n- | \n
--van-image-preview-index-font-size | \nvar(--van-font-size-md) | \n- | \n
--van-image-preview-index-line-height | \nvar(--van-line-height-md) | \n- | \n
--van-image-preview-index-text-shadow | \n0 1px 1px var(--van-gray-8) | \n- | \n
--van-image-preview-overlay-background | \nrgba(0, 0, 0, 0.9) | \n- | \n
--van-image-preview-close-icon-size | \n22px | \n- | \n
--van-image-preview-close-icon-color | \nvar(--van-gray-5) | \n- | \n
--van-image-preview-close-icon-margin | \nvar(--van-padding-md) | \n- | \n
--van-image-preview-close-icon-z-index | \n1 | \n- | \n