import{o as a,a as t,y as n}from"./vue-libs.b44bc779.js";const e={class:"van-doc-markdown-body"},l=n(`

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.

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://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',
      'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
    ];
    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>
  <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="{ active, total }">
    <div class="custom-indicator">{{ active + 1 }}/{{ total }}</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>

API

Swipe Props

AttributeDescriptionTypeDefault
autoplayAutoplay interval (ms)number | string-
durationAnimation duration (ms)number | string500
initial-swipeIndex of initial swipe, start from 0number | string0
widthWidth of swipe itemnumber | string0
heightHeight of swipe itemnumber | string0
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: index of current swipe

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:

import type { SwipeProps, SwipeInstance, SwipeToOptions } from 'vant';

SwipeInstance is the type of component instance:

import { ref } from 'vue';
import type { SwipeInstance } from 'vant';

const swipeRef = ref<SwipeInstance>();

swipeRef.value?.next();

SwipeToOptions

NameDescriptionType
immediateWhether to skip animationboolean

Swipe Slots

NameDescriptionSlotProps
defaultContent-
indicator v3.4.0Custom 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-background-colorvar(--van-primary-color)-
--van-swipe-indicator-inactive-background-colorvar(--van-border-color)-
`,20),p=[l],r={__name:"README",setup(c,{expose:s}){return s({frontmatter:{}}),(i,h)=>(a(),t("div",e,p))}};export{r as default};