小程序 Tab栏切换组件

  • 核心 主要用到了 swiper组件
属性类型说明
currentNumber当前滑块的 index 默认为0
current-item-idString当前滑块的 item-id 不能同时指定 current
bindchangeEventHandlecurrent 改变时会触发 change 事件

在这里插入图片描述

  • 效果
    在这里插入图片描述
  • wxl
<view class="wrapper">
    <view class="head">
        <view class="head-item {{active == 0 ?'bd-btom':''}}" data-tap-id="0" bindtap="toggle">1</view>
        <view class="head-item {{active == 1 ?'bd-btom':''}}" data-tap-id="1" bindtap="toggle">2</view>
        <view class="head-item {{active == 2 ?'bd-btom':''}}" data-tap-id="2" bindtap="toggle">3</view>
    </view>
    <view style="margin-top:20rpx">
        <swiper 
        bindchange="changeTab"
        current-item-id="{{active}}" >
            <swiper-item   item-id="0" style="background:yellow;">
                <view style="width:100%;height:100%;">
                    1
                </view>
            </swiper-item>
            <swiper-item item-id="1" style="background:aqua;">
                <view style="width:100%;height:100%;">
                    2
                </view>
            </swiper-item>
            <swiper-item item-id="2" style="background:rgb(0, 255, 13);">
                <view style="width:100%;height:100%;">
                    3
                </view>
            </swiper-item>
        </swiper>
    </view>
</view>
  • js
toggle(e) {
    let {
      tapId
    } = e.currentTarget.dataset
    this.setData({
      active: tapId
    })
  },

  changeTab(e) {
    let {
      current
    } = e.detail
    console.log(current);
    this.setData({
      active:current
    })
    console.log("触发");
  }
  • wxss
.wrapper,
page {
    width: 100%;
    height: 100%;
    background-color: #fff;
}

.wrapper .head {
    display: flex;
    height: 60rpx;
    text-align: center;
    line-height: 60rpx;
}

.head >view:first-child {
    background-color: red;
}
.head>view:nth-child(2){
    background-color:blue;
}
.head>view:last-child{
    background-color: aqua;
}

.bd-btom {
    border-bottom: 10rpx solid greenyellow;
}

.head-item {
    flex: 1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值