Skip to content

Swiper Carousel Component

This component is typically used for scenarios such as navigation carousels and advertisement displays. It works out of the box and has the following features:

  • Custom indicator mode with configurable indicator styles
  • 3D carousel effect to meet different development needs
  • Configurable title display, covering various application scenarios
  • Capable of setting loading states and embedding videos, offering rich and complete functionality

📌 Platform Differences

APP(vue)H5WeChat Mini ProgramAlipay Mini Program

🏯 Basic Usage Example

html
<!-- Global usage -->
<hy-swiper :list="list"></hy-swiper>
<!-- Import as a single component -->
<HySwiper :list="list"></HySwiper>
ts
import { HySwiper } from '@hy-app/ui';
import { ref } from 'vue';

const list = ref([
    'https://img0.baidu.com/it/u=1913990970,584854398&fm=253&id=1',
    'http://mms2.baidu.com/it/u=204741874,3444396868&fm=253&id=2',
    'https://img2.baidu.com/it/u=3042825715,659259329&fm=253&id=3',
    'https://img2.baidu.com/it/u=109690972,2214958998&fm=253&id=4',
    'http://mms0.baidu.com/it/u=2161107790,808970254&fm=253&id=5',
    'https://img2.baidu.com/it/u=4211554685,400408647&fm=253&id=6',
    'https://img2.baidu.com/it/u=2237681987,1998118702&fm=253&id=7',
    'https://img1.baidu.com/it/u=2494879897,1602792615&fm=253&id=8',
]);

Object Usage Instructions

Tip

  • If the array contains objects, the image is taken from the key url as the image address
  • If the array contains objects and you want to rename the key url inside the object to image, you need to set keyName to image
  • title is the text content displayed on the carousel; you must first set the carousel property showTitle to true
  • Fill in poster when the url content is a video url
  • type set to video displays a video; type set to image displays an image; images are displayed by default
html
<template>
    <hy-swiper :list="list" showTitle :autoplay="false" circular></hy-swiper>
</template>

<script setup>
    import { reactive } from 'vue';

    // Use reactive to create a reactive array of objects
    const list = reactive([
        {
            url: 'https://media.w3.org/2010/05/sintel/trailer.mp4',
            title: '昨夜星辰昨夜风,画楼西畔桂堂东',
            type: 'video',
        },
        {
            url: 'https://cdn.uviewui.com/uview/swiper/swiper1.png',
            title: '身无彩凤双飞翼,心有灵犀一点通',
        },
        {
            url: 'https://cdn.uviewui.com/uview/swiper/swiper3.png',
            title: '谁念西风独自凉,萧萧黄叶闭疏窗,沉思往事立残阳',
        },
    ]);
</script>

Loading

  • Add the loading attribute to achieve a loading state
html
<template>
    <hy-swiper :list="list" loading></hy-swiper>
</template>

Custom Indicator

  • If you need to customize the indicator in the form of dots or numbers, please refer to the following example:
Click to view the full code
vue
<template>
    <hy-swiper :list="list">
        <template #indicator="{ current, length }">
            <view class="indicator">
                <text>{{ current }}</text>
                <text>/</text>
                <text>{{ length }}</text>
            </view>
        </template>
    </hy-swiper>
</template>

<script setup>
    import { reactive } from 'vue';

    const list = reactive([
        'https://cdn.uviewui.com/uview/swiper/swiper3.png',
        'https://cdn.uviewui.com/uview/swiper/swiper2.png',
        'https://cdn.uviewui.com/uview/swiper/swiper1.png',
    ]);
</script>

<style lang="scss">
    .indicator {
        display: flex;
        flex-direction: row;
        justify-content: center;
        width: 90rpx;
        height: 40rpx;
        background: rgba(128, 128, 128, 0.7);
        border-radius: $hy-border-radius-semicircle;
        color: #fff;
    }
</style>

3D Playback

  • In actual development, an ordinary carousel may not meet your development needs. The swiper component provides an API for card-style carousels. You can refer to the following example to implement this feature
Click to view the full code
html
<template>
    <!-- #ifndef APP-NVUE || MP-TOUTIAO -->
    <view class="u-demo-block">
        <text class="u-demo-block__title">Card style</text>
        <hy-swiper
            :list="list3"
            previousMargin="30"
            nextMargin="30"
            circular
            :autoplay="false"
            radius="5"
            bgColor="#ffffff"
        ></hy-swiper>
    </view>
    <!-- #endif -->
</template>

<script setup>
    import { reactive } from 'vue';

    const list3 = reactive([
        'https://cdn.uviewui.com/uview/swiper/swiper3.png',
        'https://cdn.uviewui.com/uview/swiper/swiper2.png',
        'https://cdn.uviewui.com/uview/swiper/swiper1.png',
    ]);
</script>

API

Swiper Props

ParameterDescriptionTypeDefault Value
listCan be an array of strings or an array of objects; for objects, see "Object Usage Instructions" abovearray-
indicatorWhether to display the panel indicatorbooleanfalse
indicatorActiveColorActive color of the indicatorstring#FFFFFF
indicatorInactiveColorInactive color of the indicatorstringrgba(255, 255, 255, 0.35)
indicatorStyleIndicator style; can be positioned via bottom, left, rightCSSProperties-
indicatorModeIndicator modeline | dotline
autoplayWhether to switch automaticallybooleantrue
currentIndex of the currently displayed swiper itemnumber | string0
currentItemIditem-id of the current swiper item; cannot be specified together with currentstring-
intervalTime interval for automatic switching of swiper items (ms)number3000
durationDuration of the swiper item switching animation (ms); not supported on nvuenumber300
circularWhether to loop back to the beginning after reaching the endbooleanfalse
previousMarginFront margin, can be used to reveal a small portion of the previous item; not supported on nvue and Alipay; values default to pxstring | number0
nextMarginBack margin, can be used to reveal a small portion of the next item; not supported on nvue and Alipay; values default to pxstring | number0
accelerationWhen enabled, multiple screens will be swiped consecutively based on the swipe speed; not supported on Alipaybooleanfalse
displayMultipleItemsNumber of swiper items displayed simultaneously; not supported on nvue and Alipay Mini Programnumber1
easingFunctionSpecifies the swiper transition easing animation type; only effective on WeChat Mini Programdefault|linear|easeInCubic |easeOutCubic|easeInOutCubicdefault
keyNameProperty key name used to specify the url in the list arraystringurl
imgModeImage cropping mode; see the uniapp basic component imagestringaspectFill
heightComponent height; values default to pxstring | number130
bgColorBackground colorstring-
radiusComponent border radius; values default to pxstring | number4
loadingWhether it is in a loading statebooleanfalse
showTitleWhether to display the title; requires the title property in the array objectsbooleanfalse

Events

Event NameDescriptionCallback Parameters
clickTriggered when the carousel is clickedindex: which image was clicked, starting from 0
changeTriggered when the carousel switches (automatically or manually)index: which image was switched to, starting from 0

Slots

Slot NameDescriptionAccepted Values
default-record: object value, index: index
indicator--
03:29