【微信小程序】swiper自定义样式:指示点样式 wx-swiper-dot

本文详细介绍了如何使用Swiper组件调整指示点的颜色、形状、位置,以及如何自定义图片轮播图的样式,包括间距和选中点的样式。

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

    <view class="swiper">
      <swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
        <block wx:for="{{background}}" wx:key="*this">
          <swiper-item>
            <view class="swiper-item {{item}}"></view>
          </swiper-item>
        </block>
      </swiper>
    </view>

swiper 样式

  • .wx-swiper-dots : 指示点容器样式
  • .wx-swiper-dots-horizontal : 水平滑动的指示点容器样式,其在.wx-swiper-dots内。
  • .wx-swiper-dot :指示点样式
  • .wx-swiper-dot-active : 当前指示点样式

指示点颜色

默认指示点颜色

改变指示点颜色

.swiper .wx-swiper-dot {
  background-color: rgba(255,255,255,0.7);
}
.swiper .wx-swiper-dot-active {
  background-color:#ff4e5b;
}

在这里插入图片描述

指示点形状

默认指示点形状
在这里插入图片描述
改变指示点形状

.swiper .wx-swiper-dots-horizontal .wx-swiper-dot {
  width: 45rpx;
  height: 8rpx;
  border-radius: 5rpx;
}

指示点位置

默认指示点位置贴近底部
在这里插入图片描述
改变指示点位置

.swiper .wx-swiper-dots.wx-swiper-dots-horizontal {
  position: absolute;
  bottom: 40rpx;
}
  • .wx-swiper-dots.wx-swiper-dots-horizontal,注意中间,没有空格
    在这里插入图片描述
    或者
  • .swiper .wx-swiper-dots.wx-swiper-dots-horizontal {
      margin-bottom: 40rpx;
    }

    .wx-swiper-dots.wx-swiper-dots-horizontal,注意中间,没有空格

自定义swiper点的样式图片样式

<!-- 轮播图 -->
<view class="sy_yfgg">
    <swiper indicator-dots="true" autoplay="true" interval="2000">
        <swiper-item>
            <image class="image" src="../../images/home_topback.png" mode="aspectFill"></image>
        </swiper-item>
        <swiper-item>
            <image class="image" src="../../images/home_topback.png" mode="aspectFill"></image>
        </swiper-item>
        <swiper-item>
            <image class="image" src="../../images/home_topback.png" mode="aspectFill"></image>
        </swiper-item>
    </swiper>
</view>
/* 设置swiper组件的宽高 */
.sy_yfgg swiper{width: 95%;height: 110rpx; margin: 15rpx auto; border-radius: 20rpx; overflow: hidden;}
/* 设置swiper组件里面图片的宽高 */
.sy_yfgg swiper image{width: 100%; height: 110rpx;border-radius: 20rpx;}
/* 修改dot形状 */
.sy_yfgg .wx-swiper-dots .wx-swiper-dot{width:8rpx;height:8rpx;border-radius:4rpx;background-color:#d8d9dd}
/* 修改dot之间的间距 */
.sy_yfgg .wx-swiper-dots .wx-swiper-dot:nth-of-type(n+2){margin-left:0rpx}
/* 修改选中dot形状 */
.sy_yfgg .wx-swiper-dots .wx-swiper-dot.wx-swiper-dot-active{width:24rpx;height:8rpx;border-radius:4rpx;background-color:#fff}
/* 调成dots的位置 */
.sy_yfgg .wx-swiper-dots.wx-swiper-dots-horizontal{bottom:10rpx;text-align:center}

微信小程序中,`swiper`组件的指示样式默认是显示在滑块上的。如果需要在弹窗上显示指示器,可以通过自定义指示器来实现。具体步骤如下: 1. **隐藏默认指示器**:通过设置`indicator-dots`为`false`来隐藏默认指示器。 2. **自定义指示器**:在`swiper`组件外部添加自定义指示器元素,并通过`wx:for`循环和`wx:key`来动态生成指示器。 3. **绑定事件**:通过`bindchange`事件来更新当前页面的索引,从而控制指示器的样式。 以下是一个示例代码: ```xml <!-- wxml --> <view class="container"> <swiper indicator-dots="{{false}}" bindchange="onSwiperChange" autoplay="{{false}}" current="{{current}}" style="height: 200px;"> <block wx:for="{{images}}" wx:key="index"> <swiper-item> <image src="{{item}}" mode="aspectFill" style="width: 100%; height: 100%;" /> </swiper-item> </block> </swiper> <view class="indicator"> <view wx:for="{{images}}" wx:key="index" class="dot {{index === current ? 'active' : ''}}"></view> </view> </view> ``` ```css /* wxss */ .container { position: relative; } .indicator { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); display: flex; } .dot { width: 10px; height: 10px; margin: 0 5px; background-color: rgba(255, 255, 255, 0.5); border-radius: 50%; } .dot.active { background-color: rgba(255, 255, 255, 1); } ``` ```javascript // js Page({ data: { images: [ 'https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg' ], current: 0 }, onSwiperChange(e) { this.setData({ current: e.detail.current }); } }); ``` 在这个示例中,`swiper`组件的指示器被隐藏了,通过自定义的`dot`元素来显示指示器,并通过`current`变量来控制当前页面的指示样式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值