Swipe

introduce

Used to loop through a set of images or content.

Code Demo

Basic usage

Each SwipeItem represents a carousel card, and the indicator color can be set through the indicator-color property.

<z-swipe indicator-color="#ffffff">
   <z-swipe-item
     v-for="(item, index) in 4"
     :key="index"
     :custom-style="customStyle"
     >{{ item }}</z-swipe-item
   >
</z-swipe>
import { ref } from 'vue'
const customStyle = ref({
   color: '#ffffff',
   'font-size': '40rpx',
   'line-height': '300rpx',
   'text-align': 'center',
   'background-color': '#39a9ed'
})

Autoplay

The automatic rotation interval can be set through the autoplay attribute.

<z-swipe autoplay="1000" indicator-color="#ffffff">
   <z-swipe-item
     v-for="(item, index) in 4"
     :key="index"
     :custom-style="customStyle"
     >{{ item }}</z-swipe-item
   >
</z-swipe>
import { ref } from 'vue'
const customStyle = ref({
   color: '#ffffff',
   'font-size': '40rpx',
   'line-height': '300rpx',
   'text-align': 'center',
   'background-color': '#39a9ed'
})

Listen for change events

After each page rotation ends, the change event will be triggered.

<z-swipe @change="onChange">
   <z-swipe-item
     v-for="(item, index) in 4"
     :key="index"
     :custom-style="customStyle"
     >{{ item }}</z-swipe-item
   >
</z-swipe>
const onChange = (index: number) => console.log('Current Swipe index: ' + index)

Vertical scrolling

After setting the vertical attribute, the sliders will be arranged vertically. In this case, you need to specify the height of the slider container.

<z-swipe
   vertical
   indicator-color="#ffffff"
   :custom-style="{ height: '300rpx' }"
>
   <z-swipe-item
     v-for="(item, index) in 4"
     :key="index"
     :custom-style="customStyle"
     >{{ item }}</z-swipe-item
   >
</z-swipe>

Customize slider size

The default width of the slider is 100%, and the width of a single slider can be set through the width property. In vertical scrolling mode, the height of a single slider can be set through the height property.

<z-swipe :loop="false" :width="200">
   <z-swipe-item
     v-for="(item, index) in 4"
     :key="index"
     :custom-style="customStyle"
     >{{ item }}</z-swipe-item
   >
</z-swipe>

Customizing the slider size in loop scrolling mode is currently not supported, so loop needs to be set to false.

Custom indicator

The indicator's style can be customized via the indicator slot.

<z-swipe :show-indicators="false">
   <z-swipe-item
     v-for="(item, index) in 4"
     :key="index"
     :custom-style="customStyle"
     >{{ item }}</z-swipe-item
   >
   <template #indicator="{ active, total }">
     <view class="custom-indicator">{{ active + 1 }}/{{ total }}</view>
   </template>
</z-swipe>

<style>
.custom-indicator {
   position: absolute;
   right: 10rpx;
   bottom: 10rpx;
   padding: 4rpx 10rpx;
   font-size: 24rpx;
   color: #fff;
   background: rgb(0 0 0 / 10%);
}
</style>

API

Swipe Props

ParametersDescriptionTypeDefault value
autoplayAutomatic rotation interval, unit is msnumber | string-
durationanimation duration, unit is msnumber | string500
initial-swipeInitial position index valuenumber | string0
widthSlider width, unit is pxnumber | stringauto
heightSlider height, unit is pxnumber | stringauto
loopWhether to enable loop playbackbooleantrue
show-indicatorsWhether to show indicatorsbooleantrue
verticalWhether to scroll verticallybooleanfalse
touchableWhether swiping through gestures is possiblebooleantrue
indicator-colorindicator colorstring#1989fa

Swipe Events

Event nameDescriptionCallback parameters
changeTriggered after each page carousel endsindex: number
drag-startTriggered when the user starts dragging the carousel component
drag-endTriggered when the user ends dragging the carousel component

SwipeItem Events

Event nameDescriptionCallback parameters
clickTriggered when clickedevent: MouseEvent

Swipe method

Through ref, you can get the Swipe instance and call the instance method.

Method nameDescriptionParametersReturn value
prevSwitch to the previous carousel--
nextSwitch to the next carousel--
swipeToSwitch to the specified locationindex: number, options: SwipeToOptions-
resizeThis method can be called to trigger redrawing when the size of the outer element or the display state of the component changes--

SwipeToOptions format

NameDescriptionType
immediateWhether to skip animationboolean

Swipe Slots

NameDescriptionParameters
defaultcarousel content-
indicatorCustom indicator{ active: number, total: number }

Theme customization

Style variables

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

NameDefaultDescription
--z-swipe-indicator-size12rpx-
--z-swipe-indicator-marginvar(--z-padding-sm)-
--z-swipe-indicator-active-opacity1-
--z-swipe-indicator-inactive-opacity0.3-
--z-swipe-indicator-active-backgroundvar(--z-primary-color)-
--z-swipe-indicator-inactive-backgroundvar(--z-border-color)-