Skip to content

Swiper 轮播图

全端响应式轮播图组件,支持胶囊长点/经典小圆点/数字页码三种指示器、前后露边卡片轮播 (Peek Margin) 以及自定义插槽模板扩展。


代码示例

1. 基础用法 (Basic Usage)

vue
<template>
  <mu-swiper :list="bannerList" :height="340" @click="onClick" />
</template>

<script setup>
import { ref } from 'vue'

const bannerList = ref([
  { image: 'https://picsum.photos/800/400?random=1', title: '轮播图标题 1' },
  { image: 'https://picsum.photos/800/400?random=2', title: '轮播图标题 2' }
])

function onClick(index) {
  console.log('点击了轮播项:', index)
}
</script>

2. 指示器模式与位置 (Indicator Modes)

通过 indicator-mode 配置指示器形态(line 胶囊拉长点 | dot 小圆点 | fraction 数字页码 | none),通过 indicator-pos 调整放置位置(bottom-center | bottom-right | bottom-left)。

vue
<template>
  <!-- 数字页码右下角模式 -->
  <mu-swiper
    :list="bannerList"
    indicator-mode="fraction"
    indicator-pos="bottom-right"
  />
</template>

3. 前后露边卡片轮播 (Peek Margin)

设置 previous-marginnext-margin 可以实现卡片前后露出边角的效果。

vue
<template>
  <mu-swiper
    :list="bannerList"
    previous-margin="30rpx"
    next-margin="30rpx"
    indicator-mode="dot"
  />
</template>

4. 自定义插槽内容 (Custom Slot)

使用默认插槽解构 :item:index 来自定义卡片内部结构:

vue
<template>
  <mu-swiper :list="cardList" :height="220" indicator-mode="none">
    <template #default="{ item, index }">
      <view class="custom-card">
        <text>{{ item.title }}</text>
      </view>
    </template>
  </mu-swiper>
</template>

API 属性说明 (Props)

参数名说明类型可选值默认值
list轮播图片或数据列表(支持图片 URL 数组或对象数组)Array-[]
current当前显示的轮播项索引Number-0
height轮播图容器高度 (rpx / px)Number | String-360
interval自动切换时间间隔 (ms)Number-3000
duration滑动动画切换时长 (ms)Number-300
autoplay是否开启自动播放Booleantrue | falsetrue
loop是否开启无缝循环播放Booleantrue | falsetrue
indicator是否显示指示器Booleantrue | falsetrue
indicatorMode指示器形态Stringline | dot | fraction | none'line'
indicatorPos指示器摆放位置Stringbottom-center | bottom-right | bottom-left'bottom-center'
indicatorColor激活态指示器主题色String-''
imageMode图片裁剪填充模式String'aspectFill' | 'scaleToFill''aspectFill'
radius轮播图内图片圆角String-'24rpx'
previousMargin前边距 (用于露出前卡片边角)String-'0px'
nextMargin后边距 (用于露出后卡片边角)String-'0px'

事件说明 (Events)

事件名说明回调参数
change切换轮播项时触发(current: number)
click点击某个轮播项时触发(index: number)

插槽说明 (Slots)

插槽名说明作用域参数
default自定义轮播项卡片内部内容{ item: any, index: number }

基于 MIT 协议开源发行

H5 模拟器 (swiper)☀️🔄