目录

HarmonyOS鸿蒙学习笔记8Swiper实现轮播滚动效果

目录

HarmonyOS鸿蒙学习笔记(8)Swiper实现轮播滚动效果

HarmonyOS提供了 组件实现轮播图效果:

https://i-blog.csdnimg.cn/blog_migrate/e4788d9cebff1ec0a3019990d8764c18.png

实现代码如下:

  Swiper() {
        Image($rawfile('computer/computer1.png'))
          .height(200)
          .width('100%')
        Image($rawfile('computer/computer2.png'))
          .height(200)
          .width('100%')
        Image($rawfile('computer/computer3.png'))
          .height(200)
          .width('100%')
        Image($rawfile('computer/computer4.png'))
          .height(200)
          .width('100%')
      }
      .index(0)
      .autoPlay(true)//自动播放
      .interval(2000)
      .indicatorStyle({
        color: Color.Gray,//未选中的颜色
        selectedColor:Color.Red//设置选中的导航点的颜色。
      })
      .loop(true)//开启循环轮播
      .height(250)
      .width('100%')

如果想让导航点在上面展示(如下图),该怎么办呢?

https://i-blog.csdnimg.cn/blog_migrate/1d5eb550cfba829058df414fb72caad0.png

很简单,修改 indicatorStyletop 值,代码如下:

  .indicatorStyle({
        color: Color.Gray,//未选中的颜色
        selectedColor:Color.Red,//设置选中的导航点的颜色。
        top:0//设置导航点距离Swiper组件顶部的距离。
      })

另外 indicatorStyletopbottom 属性感觉有点迷惑,设置了大于0的值感觉达不到理论计算的位置。

参考资料: