Swipe
Install
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.
<van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
<van-swipe-item>1</van-swipe-item>
<van-swipe-item>2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
</van-swipe>
<style>
.my-swipe .van-swipe-item {
color: #fff;
font-size: 20px;
line-height: 150px;
text-align: center;
background-color: #39a9ed;
}
</style>
Lazy Render
Use lazy-render
prop to enable lazy rendering.
<van-swipe :autoplay="3000" lazy-render>
<van-swipe-item v-for="image in images" :key="image">
<img :src="image" />
</van-swipe-item>
</van-swipe>
export default {
setup() {
const images = [
'https://img.yzcdn.cn/vant/apple-1.jpg',
'https://img.yzcdn.cn/vant/apple-2.jpg',
];
return { images };
},
};
Change Event
<van-swipe @change="onChange">
<van-swipe-item>1</van-swipe-item>
<van-swipe-item>2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
</van-swipe>
import { Toast } from 'vant';
export default {
setup() {
const onChange = (index) => Toast('Current Swipe index:' + index);
return { onChange };
},
};
Vertical Scrolling
<van-swipe :autoplay="3000" vertical>
<van-swipe-item>1</van-swipe-item>
<van-swipe-item>2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
</van-swipe>
Set SwipeItem Size
<van-swipe :loop="false" :width="300">
<van-swipe-item>1</van-swipe-item>
<van-swipe-item>2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
</van-swipe>
It's not supported to set SwipeItem size in the loop mode.
Custom Indicator
<van-swipe @change="onChange">
<van-swipe-item>1</van-swipe-item>
<van-swipe-item>2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
<template #indicator>
<div class="custom-indicator">{{ current + 1 }}/4</div>
</template>
</van-swipe>
<style>
.custom-indicator {
position: absolute;
right: 5px;
bottom: 5px;
padding: 2px 5px;
font-size: 12px;
background: rgba(0, 0, 0, 0.1);
}
</style>
import { ref } from 'vue';
export default {
setup() {
const current = ref(0);
const onChange = (index) => {
current.value = index;
};
return {
current,
onChange,
};
},
};
API
Swipe Props
Attribute |
Description |
Type |
Default |
autoplay |
Autoplay interval (ms) |
number | string |
- |
duration |
Animation duration (ms) |
number | string |
500 |
initial-swipe |
Index of initial swipe, start from 0 |
number | string |
0 |
width |
Set Swiper Item Width |
number | string |
0 |
height |
Set Swiper Item Height |
number | string |
0 |
loop |
Whether to enable loop |
boolean |
true |
show-indicators |
Whether to show indicators |
boolean |
true |
vertical |
Whether to be vertical Scrolling |
boolean |
false |
touchable |
Whether to allow swipe by touch gesture |
boolean |
true |
stop-propagation |
Whether to stop touchmove event propagation |
boolean |
false |
lazy-render |
Whether to enable lazy render |
boolean |
false |
indicator-color |
Indicator color |
string |
#1989fa |
Swipe Events
Event |
Description |
Arguments |
change |
Emitted when current swipe changed |
index: index of current swipe |
SwipeItem Events
Event |
Description |
Arguments |
click |
Emitted when component is clicked |
event: MouseEvent |
Swipe Methods
Use ref to get Swipe instance and call instance methods..
Name |
Description |
Attribute |
Return value |
prev |
Swipe to prev item |
- |
- |
next |
Swipe to next item |
- |
- |
swipeTo |
Swipe to target index |
index: target index, options: Options |
- |
resize |
Resize Swipe when container element resized or visibility changed |
- |
- |
swipeTo Options
Name |
Description |
Type |
immediate |
Whether to skip animation |
boolean |
Swipe Slots
Name |
Description |
default |
Content |
indicator |
Custom indicator |
Less Variables
How to use: Custom Theme.
Name |
Default Value |
Description |
@swipe-indicator-size |
6px |
- |
@swipe-indicator-margin |
@padding-sm |
- |
@swipe-indicator-active-opacity |
1 |
- |
@swipe-indicator-inactive-opacity |
0.3 |
- |
@swipe-indicator-active-background-color |
@blue |
- |
@swipe-indicator-inactive-background-color |
@border-color |
- |