Swiper
Data-driven carousel with autoplay, looping, vertical direction and custom indicators.
Import
Use <hui-swiper> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<template>
<view>
<hui-space direction="vertical" fill>
<hui-swiper
v-model="current"
custom-class="hui-space__item"
:items="items"
:height="180"
indicator-type="fraction"
:autoplay="false"
@change="(value, context) => (lastEvent = value + ':' + context.source)"
@click="onClick"
/>
<hui-swiper
custom-class="hui-space__item"
:items="items"
:height="180"
direction="vertical"
:interval="4000"
:previous-margin="12"
:next-margin="12"
/>
<text class="hui-space__item">{{ current }} / {{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { SwiperItem } from '@nscode/handy-uni'
const lastEvent = ref('尚无事件 / No event')
const current = ref(0)
const onClick = (item: string | SwiperItem, context: { index: number }) => {
lastEvent.value = JSON.stringify({ item, context })
}
const items = [
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAgCAIAAADbtmxLAAAB/0lEQVR4nM3QVUIUABAA0DmEgCDYgoCBqEgoiIiBSkp3d9d2dy/d3d3d3SWooBJ2YAL/XmPeER4ccyaoupKOu1M0PGma3oyTvqzT/pyzgbzzwQLtUNHFcIlepOxStOJKbJpBfIZhYtaN5Byj1DxjYoEpueg2tcScXnaXWXGPXXWfW/OAX/dI2GAjbnoqbbGVt9krOxzTu55n9rhk97nlDnjkD3kVjvgUj/mVTgSUTwVVzoRUz4XVLkTUL0U1rsQ0r8W1rie0vwQVF6KaG1ndg3rCi67lwzzlxz4TwD0XxL8QItQJE+tGSPWj5JdjlFfj0q8lZF5Pyr6ZknuLkG9CKjSjFN+hlVowyi1ZlVacamte7UNB/WNR4xNJ8zNZq52i3SGt0ymj2zmr1zWn3z1v0LNg2Lto1Ldk3L9sMrBiOrhqNrRmPrxuMbJhObppNbblRXzbRmLHK0DVk9T5GlD1JHdtAqqelO4tQNWT2vMGUPUQet8Cqh5i3ztA1UPq3wZUPeSBHUDVQxncBVQ91KE9QNVDG34PqHroIx8AVQ9j9COg6mGOfQJUPazxz4Cqhz3xBVD1cCa/Aqoe7tQ3QNXDm/4OqHr4Mz8AVY9gdh9Q9QjnfgKqHtH8L0DVI174Dah6JIt/AFWPdOkvoOqRLf8DVD3ylQNA1aNYPQRUPcq1o/83VquIElJ47QAAAABJRU5ErkJggg==',
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAgCAIAAADbtmxLAAAB30lEQVR4nM3QA2IcABAAwP1Qbdt2G+Ns27Zt5+5i27aTGj/pN3aeMFBzglB3itRwhtJ0jtZygUG4xCJd4VCu8Wg3BIxbItYdCeeejPdAIXikEj3RSJ7pZC8Milcm1RuL5p1N98Fh+OQyffFYan22+oCjMeRqjnhaYz5iIkBOhaiZCD0XYxYS7FKKW87wqzlhR0HcVZL2lOV9VeVAh3qoSzvSox/rM04MmKeGrDMj9jmoPUmsP01uPEttPk9vvcgkXmaTr3Kp1/n0m0LmbTH7rpR7X85/qBQ+VoufaqXP9fKXRuVrs/qtVfverv/oNH52m2u81jq/vSHobAq7W6JeQtxPSgYp6TAtG2Xk46xiktOW5lWygva8qLMo6W6T9VYU/e2qwU7NcLdutNcw3m+aHLRMD9tmRx3zgKpnzLkAqHrGXYuAqmfCvQSoeiY9y4CqZ8q7Aqh6pn2rgKpnxr8GqHpmA+uAqmcuuAGoeuZDm4CqZyG8Bah6FiPbgKpnKboDqHqWY7uAqmclvgeoelYT+4CqZy15AKh61lOHgKpnI30EqHo2M8eAqmcr+xVQ9WznvgGqnp38d0DVs1v4Aah69oo/AVXPfukXoOo5aPsNqHoOy38AVc9R5S+g6jmu/vsPM1TniEVFeWsAAAAASUVORK5CYII='
]
</script>
Props
| Prop | Type | Default | Details |
|---|---|---|---|
items | (string | SwiperItem)[] | [] | Data items rendered in source order. |
modelValue | number | — | Controlled current value; accept update:modelValue to change it. |
defaultValue | number | 0 | Initial uncontrolled value. |
height | number | string | 180 | Height; numbers use px and strings preserve their unit. |
autoplay | boolean | false | Starts autoplay; disabled automatically for fewer than two items. |
interval | number | 3000 | Autoplay or rotation interval in ms. |
duration | number | 300 | Animation or display duration in milliseconds; see platform notes for zero. |
loop | boolean | true | Loops from the final slide to the first. |
direction | 'horizontal' | 'vertical' | 'horizontal' | Layout or scrolling direction. |
indicatorPosition | SwiperIndicatorPosition | 'inside' | Indicator placement; none hides it. |
indicatorType | SwiperIndicatorType | 'dots' | Dot or fraction indicator. |
previousMargin | number | 0 | Visible margin before the active slide, in px. |
nextMargin | number | 0 | Visible margin after the active slide, in px. |
imageFit | 'fill' | 'contain' | 'cover' | 'cover' | Default slide image fit. |
preview | boolean | false | Enables image preview. |
disabled | boolean | false | Disables user interaction. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
update:modelValue | (index: number) |
change | (index: number, context: { source: SwiperChangeSource }) |
click | (item: string | SwiperItem, context: { index: number }) |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
default | { item, index, active } |
indicator | { current, total } |
Methods
| Method | Signature | Description |
|---|---|---|
setActiveItem | (index: number): void | Select a slide. |
prev | (): void | Select the previous slide. |
next | (): void | Select the next slide. |
Types
SwiperItem、SwiperChangeSource、SwiperIndicatorPosition、SwiperIndicatorType、SwiperProps、SwiperEmits、SwiperExpose.
Platform notes
Mini-program and App use native swiper and preserve event sources. Ordinary Web uses touch-driven slides and boundary clones, pausing autoplay during touches, focus and deactivation. Controlled state reverts when the parent does not accept an update. Margins, vertical gestures and fast swipes require device checks.
Device smoke checklist.