Skip to content

Swipe - Vant 4

Swipe

Intro

Used to loop a group of pictures or content.

Install

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

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

Usage

Basic Usage

Use autoplay prop to set autoplay interval.

html

Lazy Render

Use lazy-render prop to enable lazy rendering.

html
js
exportdefault { setup() { const images = [ 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg', 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg', ]; return { images }; }, };

Change Event

html
js
import { showToast } from'vant'; exportdefault { setup() { constonChange = (index) => showToast('Current Swipe index:' + index); return { onChange }; }, };

Vertical Scrolling

html

Set SwipeItem Size

html

It's not supported to set SwipeItem size in the loop mode.

Custom Indicator

html

API

Swipe Props

AttributeDescriptionTypeDefault
autoplayAutoplay interval (ms)*numberstring*
durationAnimation duration (ms)*numberstring*
initial-swipeIndex of initial swipe, start from 0*numberstring*
widthWidth of swipe item*numberstring*
heightHeight of swipe item*numberstring*
loopWhether to enable loopbooleantrue
show-indicatorsWhether to show indicatorsbooleantrue
verticalWhether to be vertical Scrollingbooleanfalse
touchableWhether to allow swipe by touch gesturebooleantrue
stop-propagationWhether to stop touchmove event propagationbooleanfalse
lazy-renderWhether to enable lazy renderbooleanfalse
indicator-colorIndicator colorstring#1989fa

Swipe Events

EventDescriptionArguments
changeEmitted when current swipe changedindex: number
drag-start v4.0.9Emitted when user starts dragging the swipe{ index: number }
drag-end v4.0.9Emitted when user ends dragging the swipe{ index: number }

SwipeItem Events

EventDescriptionArguments
clickEmitted when component is clickedevent: MouseEvent

Swipe Methods

Use ref to get Swipe instance and call instance methods..

NameDescriptionAttributeReturn value
prevSwipe to prev item--
nextSwipe to next item--
swipeToSwipe to target indexindex: number, options: SwipeToOptions-
resizeResize Swipe when container element resized or visibility changed--

Types

The component exports the following type definitions:

ts
importtype { SwipeProps, SwipeInstance, SwipeToOptions } from'vant';

SwipeInstance is the type of component instance:

ts
import { ref } from'vue'; importtype { SwipeInstance } from'vant'; const swipeRef = ref<SwipeInstance>(); swipeRef.value?.next();

SwipeToOptions

NameDescriptionType
immediateWhether to skip animationboolean

Swipe Slots

NameDescriptionSlotProps
defaultContent-
indicatorCustom indicator{ active: number, total: number }

Theming

CSS Variables

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

NameDefault ValueDescription
--van-swipe-indicator-size6px-
--van-swipe-indicator-marginvar(--van-padding-sm)-
--van-swipe-indicator-active-opacity1-
--van-swipe-indicator-inactive-opacity0.3-
--van-swipe-indicator-active-backgroundvar(--van-primary-color)-
--van-swipe-indicator-inactive-backgroundvar(--van-border-color)-

Enterprise-level mobile solution based on Vant