Skip to content

ImagePreview - Vant 4

ImagePreview

🖼️ Transform your images into immersive, full-screen experiences! Give your users the power to explore every detail with smooth zooming, elegant transitions, and intuitive gestures. Perfect for galleries, product showcases, and any visual content that deserves the spotlight.

🎯 Intro

Welcome to the ultimate image viewing experience! Our ImagePreview component brings professional-grade image browsing to your Vue applications. Whether you need quick function calls for instant previews or full component integration for custom experiences, we've got you covered. Zoom, swipe, and explore images like never before! ✨

📦 Install

Ready to elevate your image viewing experience? Getting started is incredibly simple! Register the ImagePreview component globally via app.use and unlock powerful image preview capabilities throughout your application. Check out our Component Registration guide for alternative setup methods.

js
import { createApp } from'vue'; import { ImagePreview } from'vant'; const app = createApp(); app.use(ImagePreview);

⚡ Function Call

Experience the magic of instant image previews! Vant provides powerful utility functions that can instantly summon beautiful, full-screen image galleries with just a single line of code.

For example, calling the showImagePreview function will render a stunning image preview directly on your page - no complex setup required!

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

🎨 Usage

🌟 Basic Usage

Creating stunning image galleries has never been easier! When calling showImagePreview, simply pass an array of image URLs and watch as your images transform into an immersive, swipeable gallery experience.

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

🎯 Set Start Position

Take control of your user's journey! showImagePreview supports advanced configuration options, allowing you to specify exactly which image should be displayed first through the startPosition option. Perfect for highlighting specific content or continuing from where users left off.

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

❌ Show Close Icon

Give users complete control over their viewing experience! Enable the closeable option to display an elegant close icon in the top-right corner. Customize the icon style with close-icon and position it perfectly using close-icon-position - because every detail matters in great UX design!

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

🎧 Close Event

Stay connected with your users' actions! Listen to the close event through the onClose option and trigger custom behaviors, analytics, or feedback. Perfect for tracking user engagement or providing contextual responses.

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

🛡️ Before Close

Take control of the closing process! Use the beforeClose callback to perform validation, save user progress, or show confirmation dialogs before the preview closes. This powerful feature ensures users never lose their place unexpectedly.

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

🧩 Use ImagePreview Component

Unlock unlimited customization possibilities! When you need to embed custom components, overlays, or interactive elements inside the ImagePreview, use the component approach with powerful slot customization. The index slot gives you complete creative freedom to design unique viewing experiences.

html
js
import { ref } from'vue'; exportdefault { setup() { const show = ref(false); const index = ref(0); const images = [ 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg', 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg', ]; constonChange = (newIndex) => { index.value = newIndex; }; return { show, index, images, onChange, }; }, };

🎬 Use Image Slot

Break free from traditional image constraints! The image slot opens up a world of possibilities - display videos, interactive content, or any custom media you can imagine. Set close-on-click-image to false for interactive content to prevent accidental closures and create truly engaging experiences.

html
js
import { ref } from'vue'; exportdefault { setup() { const show = ref(false); const images = [ 'https://www.w3school.com.cn/i/movie.ogg', 'https://www.w3school.com.cn/i/movie.ogg', 'https://www.w3school.com.cn/i/movie.ogg', ]; return { show, images, }; }, };

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.

html

🔧 API

⚙️ Methods

Vant exports powerful ImagePreview utility functions to give you complete programmatic control:

NameDescriptionAttributeReturn value
showImagePreviewShow image previewoptions | string[]imagePreview instance

🎛️ ImagePreviewOptions

Configure every aspect of your image preview experience with these comprehensive options:

AttributeDescriptionTypeDefault
imagesImages URL liststring[][]
startPositionStart positionnumber | string0
swipeDurationAnimation duration (ms)number | string300
showIndexWhether to show indexbooleantrue
showIndicatorsWhether to show indicatorsbooleanfalse
loopWhether to enable loopbooleantrue
onCloseTriggered when closingFunction-
onScaleTriggered when scalingFunction-
beforeCloseCallback function before closeFunction-
closeableWhether to show close iconbooleanfalse
closeIconClose icon namestringclear
closeIconPositionClose icon positionstringtop-right
transitionTransition, equivalent to name prop of transitionstringvan-fade
overlayClassCustom overlay classstring | Array | object-
overlayStyleCustom overlay styleobject-
maxZoomMax zoomnumber | string3
minZoomMin zoomnumber | string1/3
closeOnClickImageWhether to close when clicking imagebooleantrue
closeOnClickOverlayWhether to close when clicking overlaybooleantrue
verticalWhether to enable vertical gesturebooleanfalse

🎯 Props

Fine-tune your ImagePreview component with these powerful properties:

AttributeDescriptionTypeDefault
v-model:showWhether to show ImagePreviewbooleanfalse
imagesImages URL liststring[][]
startPositionStart positionnumber | string0
swipeDurationAnimation duration (ms)number | string300
showIndexWhether to show indexbooleantrue
showIndicatorsWhether to show indicatorsbooleanfalse
loopWhether to enable loopbooleantrue
beforeCloseCallback function before closeFunction-
closeableWhether to show close iconbooleanfalse
closeIconClose icon namestringclear
closeIconPositionClose icon positionstringtop-right
transitionTransition, equivalent to name prop of transitionstringvan-fade
overlayClassCustom overlay classstring | Array | object-
overlayStyleCustom overlay styleobject-
maxZoomMax zoomnumber | string3
minZoomMin zoomnumber | string1/3
closeOnClickImageWhether to close when clicking imagebooleantrue
closeOnClickOverlayWhether to close when clicking overlaybooleantrue
verticalWhether to enable vertical gesturebooleanfalse

📡 Events

Stay connected with user interactions through these comprehensive event handlers:

EventDescriptionArguments
closeTriggered when closing-
closedTriggered when closed-
changeTriggered when current image changedindex: number
scaleTriggered when scalingonScale Parameters
longPressTriggered when long pressonScale Parameters

🛠️ Methods

Control your ImagePreview programmatically with these powerful methods:

NameDescriptionAttributeReturn value
swipeToSwipe to target indexindex: number, options?: SwipeToOptions-

📝 Types

The component exports comprehensive type definitions for enhanced TypeScript development:

ts
importtype { ImagePreviewProps, ImagePreviewOptions, ImagePreviewInstance, ImagePreviewScaleEventParams, } from'vant';

ImagePreviewInstance is the type of component instance:

ts
import { ref } from'vue'; importtype { ImagePreviewInstance } from'vant'; const imagePreviewRef = ref<ImagePreviewInstance>(); imagePreviewRef.value?.swipeTo(1);

🎰 Slots

Customize every aspect of your image preview with these powerful slot options:

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 }

🔒 onClose Parameters

Access detailed information when the preview closes:

AttributeDescriptionType
urlURL of current imagenumber
indexIndex of current imagenumber

🔍 onScale Parameters

Monitor scaling interactions with precise data:

AttributeDescriptionType
indexIndex of current imagenumber
scalescale of current imagenumber

🎨 Theming

🎯 CSS Variables

Transform your image preview's appearance with these comprehensive styling variables. Refer to ConfigProvider component for global theming options.

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-
  • Image - Display and lazy load images
  • Swipe - Touch slider component
  • Popup - Popup layer component
  • Overlay - Create overlay masks
  • Icon - Icon component for close buttons
  • Toast - Show feedback messages
  • ConfigProvider - Global configuration
  • Lazyload - Image lazy loading directive

Enterprise-level mobile solution based on Vant