Skip to content

Swiper 轮播

介绍

用于循环播放一组图片或内容。

基础用法

每个 SwiperItem 组件代表一张轮播卡片,通过 height 属性设置轮播高度。

html
<vd-swiper :height="200" :vertical="state.vertical" @touchmove.stop.prevent>
  <vd-swiper-item>
    <image src="图片1" mode="aspectFill" style="width:100%;height:100%;" />
  </vd-swiper-item>
  <vd-swiper-item>
    <image src="图片2" mode="aspectFill" style="width:100%;height:100%;" />
  </vd-swiper-item>
  <vd-swiper-item>
    <image src="图片3" mode="aspectFill" style="width:100%;height:100%;" />
  </vd-swiper-item>
</vd-swiper>

纵向滚动

通过 vertical 属性设置纵向滚动。

html
<vd-swiper :height="200" vertical @touchmove.stop.prevent>
  <vd-swiper-item>1</vd-swiper-item>
  <vd-swiper-item>2</vd-swiper-item>
  <vd-swiper-item>3</vd-swiper-item>
</vd-swiper>

指示器样式

通过 indicator-type 属性设置指示器样式,可选值为 dots dots-bar page

html
<vd-swiper :height="200" indicator-type="page" @touchmove.stop.prevent>
  <vd-swiper-item>1</vd-swiper-item>
  <vd-swiper-item>2</vd-swiper-item>
  <vd-swiper-item>3</vd-swiper-item>
</vd-swiper>

指示器位置

通过 indicator-position 属性设置指示器位置,可选值为 bottom bottom-left bottom-right top top-left top-right right right-top right-bottom left left-top left-bottom right

html
<vd-swiper
  :height="200"
  indicator-type="page"
  indicator-position="bottom-right"
  @touchmove.stop.prevent
>
  <vd-swiper-item>1</vd-swiper-item>
  <vd-swiper-item>2</vd-swiper-item>
  <vd-swiper-item>3</vd-swiper-item>
</vd-swiper>

自动高度

通过 autosize 属性设置自动高度,设置后切换时会根据下一张卡片的高度自动调整。

html
<vd-swiper autosize @touchmove.stop.prevent>
  <vd-swiper-item>1</vd-swiper-item>
  <vd-swiper-item>2</vd-swiper-item>
  <vd-swiper-item>3</vd-swiper-item>
</vd-swiper>

卡片式轮播

卡片式轮播由样式实现:容器设置 overflow: visible 并留出左右边距,侧边卡片缩小并降低透明度,激活项通过 SwiperItemis-active 类还原。

html
<vd-swiper
  custom-class="swiper-card"
  :height="200"
  indicator-type="dots-bar"
  @touchmove.stop.prevent
>
  <vd-swiper-item
    v-for="(src, index) in cardList"
    :key="src"
    :index="index"
  >
    <view class="swiper-card-item">
      <image :src="src" mode="aspectFill" />
    </view>
  </vd-swiper-item>
</vd-swiper>
ts
const cardList = [
  '图片1',
  '图片2',
  '图片3',
];
scss
.swiper-card {
  overflow: visible;
  padding: 0 24px;

  &-item {
    height: 100%;
    border-radius: 12px;
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.2);
    overflow: hidden;
    transition: transform 0.3s ease, opacity 0.3s ease;
    transform: scale(0.9);
    opacity: 0.7;

    image {
      width: 100%;
      height: 100%;
    }
  }

  :deep(.vd-swiper-item.is-active) {
    z-index: 3;

    .swiper-card-item {
      transform: scale(1);
      opacity: 1;
    }
  }
}

绑定激活项

通过 active 绑定轮播当前的激活索引。

html
<vd-swiper v-model:active="active" @touchmove.stop.prevent>
  <vd-swiper-item>1</vd-swiper-item>
  <vd-swiper-item>2</vd-swiper-item>
  <vd-swiper-item>3</vd-swiper-item>
</vd-swiper>
ts
const active = ref(0);

API

Swiper Props

参数说明类型默认值
active激活索引number0
height轮播高度string | number-
autosize是否自适应轮播高度,传入数字可设置高度未计算完成的轮播初始高度boolean | string | numbertrue
autosize-begin自适应高度未计算完成时的初始高度,设置为 -1 时禁用string | number0
vertical是否纵向轮播booleanfalse
autoplay自动轮播间隔,0false 时禁用string | number | boolean5000
duration切换动画时长number500
loop是否循环播放booleantrue
swipeable是否可滑动booleantrue
swipe-limit滑动限制string | number-
indicator是否展示指示器booleantrue
indicator-type指示器类型SwiperIndicatorTypedots
indicator-position指示器位置SwiperIndicatorPosition横向滚动:'bottom' 垂直滚动:'right'

SwiperItem Props

参数说明类型默认值
index索引,小程序下循环列表时需要设置该值string | number-

代替原始 Props

参数说明类型默认值
custom-id根节点idstring-
custom-class根节点类名ClassValue-
custom-style根节点样式StyleValue-

Swiper Events

事件名说明回调参数
click点击时触发MouseEvent
change轮播切换时触发SwiperOnChangeEvent
update:active激活索引更新时触发number

SwiperItem Events

事件名说明回调参数
click点击时触发MouseEvent

Swiper Slots

名称说明
default轮播卡片内容
indicator自定义指示器,作用域参数 activevaluetotaldata
indicator-page页码指示器内容

SwiperItem Slots

名称说明
default自定义内容

Swiper Expose

方法名说明参数返回值
prev切换上一个--
next切换下一个--
swipeTo切换到指定索引index: number-

类型定义

组件导出以下类型定义:

ts
import type {
  SwiperOnChangeEvent,
  SwiperIndicatorType,
  SwiperIndicatorPosition,
  SwiperExpose,
  SwiperInstance,
} from 'vital-design';

赣ICP备2025061025号-1