目录

Swiper组件导航点颜色重叠问题

问题现象

Swiper组件中圆点指示器会出现选中页面的导航点和选中前颜色重合的情况。

指示器设置如下:

export class YDSwiperIndicatorStyles implements AttributeModifier<SwiperAttribute> {
  // 通过构造函数,创建时传参
  constructor() {
  }

  applyNormalAttribute(instance: SwiperAttribute): void {
    // instance为Button的属性对象,可以通过instance对象对属性进行修改
    instance
      .indicator(
        Indicator.dot()
          .left(0)
          .bottom(0)
          .itemWidth(15)
          .itemHeight(15)
          .color('#33000000')
          .selectedItemWidth(28)
          .selectedItemHeight(15)
          .selectedColor('#80000000')
      )
  }
}

效果如下图:

背景知识

问题定位

定义圆点导航点的color和selectedColor时颜色设置了透明度,导致在颜色叠加后会出现重合现象。

分析结论

修改建议

设置selectedColor时,选取非透明颜色可以避免出现颜色叠加,比如设置为selectedColor('#FFA8A8A8'),也可以使用selectedColor(Color.Gray)达到相同效果。

完整代码如下:

class YDSwiperIndicatorStyles implements AttributeModifier<SwiperAttribute> {
  // 通过构造函数,创建时传参
  constructor() {
  }

  applyNormalAttribute(instance: SwiperAttribute): void {
    // instance为Button的属性对象,可以通过instance对象对属性进行修改
    instance
      .indicator(
        Indicator.dot()
          .left(0)
          .bottom(0)
          .itemWidth(15)
          .itemHeight(15)
          .color('#33000000')
          .selectedItemWidth(28)
          .selectedItemHeight(15)
          .selectedColor(Color.Gray)
      );
  }
}

@Entry
@Component
struct swiperDemo {
  build() {
    Column() {
      Swiper() {
        Repeat(['1', '2', '3'])
          .each(() => {
            Column() {
              Text('测试文字A')
                .fontColor('#0A59F7')
                .margin({ bottom: 10 });
              Text('测试文字B')
                .fontColor('#0A59F7');
            }.height(90);
          });
      }
      .width('30%')
      .attributeModifier(new YDSwiperIndicatorStyles());
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center);
  }
}

效果参考如下: