本文原创发布在华为开发者社区

介绍

本示例使用Swiper(滑块视图容器)实现卡片轮播的功能。

实现卡片轮播源码链接

效果预览

请添加链接描述

使用说明

卡片数据来自预置用例,可根据实际情况自行修改。同时可以初始化偏移量列表。

实现思路

首先设置卡片数据源,以及卡片索引值,之后通过Swiper实现卡片轮播。核心代码如下:


Swiper() {
  LazyForEach(this.data, (item: CardInfo, index: number) => {
    CardComponent({
      cardInfo: item,
      cardTotalLength: this.data.totalCount(),
      cardIndex: index,
      showingCard: this.currentSwiperIndex
    })
  })
}
.index($$this.currentSwiperIndex)
  .loop(true)
  .indicator(false)
  .itemSpace(20)
  .displayCount(3)
  .duration(Constants.DURATION)
  .curve(Curve.Friction)
  .onChange((index) => {
    this.currentSwiperIndex = index
  })
  .height('100%')

鸿蒙场景化代码
1 声望0 粉丝