(Taro篇)如何自定义小程序Swiper面板指示点的样式

该博客介绍了如何在React项目中利用Taro库构建一个轮播组件,包括设置基本属性如自动播放、切换动画等,并且详细展示了如何自定义指示点的样式和交互。示例代码中包含了jsx和对应的CSS样式,用于实现圆角轮播效果和橙色高亮的指示点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

在这里插入图片描述

轮播组件jsx

import {Component} from 'react'
import {Swiper, SwiperItem, Image, View} from '@tarojs/components'
import './index.scss'

export default class SwiperBanner extends Component {

  static defaultProps = {
    list: []
  }
  state = {
    currentIndex:0
  }
  onChange(e){
    this.setState({currentIndex:e.detail.current})
  }
  render() {
    const {list} = this.props
    const {currentIndex} = this.state
    return (
      <View>
        <Swiper
          className='swiper-body'
          indicatorColor='#999'
          indicatorActiveColor='#333'
          current={currentIndex}
          duration={500}
          interval={5000}
          circular
          autoplay
          // indicatorDots //隐藏原始指示点
          onChange={this.onChange.bind(this)}
        >
          {list.map((item, index) => (
            <SwiperItem key={index}>
              <Image src={item} className='slide-image' mode='widthFix' />
            </SwiperItem>
          ))}
        </Swiper>
        // 添加自定义指示点
        <View className='spot-pagination'>
          {list.map((item, index) => (
            <View key={index} className={'spot-pagination-bullet ' + ((currentIndex==index)?'spot-pagination-bullet-active':'')}></View>
          ))}
        </View>
      </View>
    )
  }
}

CSS样式

.swiper-body {
  height: 200PX;
  border-radius: 30px !important;
  overflow: hidden;
  .slide-image{
    height: 180PX;
    width: 100%;
    border-radius: 30px !important;
    overflow: hidden;
  }
}
// 指示点样式
.spot-pagination {
  margin-top: 10PX;
  display: flex;
  justify-content: center;
  .spot-pagination-bullet{
    margin-right: 5PX;
    border-radius: 20%;
    height: 6PX;
    width: 30PX;
    background: #ccc
  }
  // 当前指示点样式
  .spot-pagination-bullet-active {
    background: orange;
  }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐言TalkMu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值