Tabs选中效果

这篇博客详细介绍了如何在微信小程序中实现tabs组件,包括wxml结构、wxss样式以及js函数处理。通过示例代码展示了如何绑定点击事件、设置默认选中项,并利用dataset传递参数。同时,文章还提供了tabs的样式设计,如颜色、字体加粗等,以达到选中和未选中状态的视觉区分。
摘要由CSDN通过智能技术生成

在这里插入图片描述

tabs的wxml写法

<view class="container">
    <view class="tabs">
        <!-- bind:tap="handleTabChange"点击事件监听
                    wx:key="index"给点击事件指定Key
                    data-index:data-是固定的,index是自己定义的,
                    要和调用的地方保持一致
                    const index = event.currentTarget.dataset.index
                    dataset后的index要和data-index后的index一致-->
        <view class="tabs-item"
              wx:for="{{tabs}}"
              wx:key="index"
              bind:tap="handleTabChange"
              data-index="{{index}}">
            <!--            Tab选中时使用tab-label样式,未选中时样式为active-tab-->
            <view class="tab-label {{currentTabIndex === index ? 'active-tab':''}}">
                {{item}}
            </view>
            <!--            逻辑控制-->
            <view class="divider" wx:if="{{currentTabIndex===index}}"></view>
        </view>
    </view>
</view>

wxss中样式

.container{
    /*弹性布局样式*/
    display: flex;
    /*布局方式列*/
    flex-direction: column;
    height: 100%;
}

.tabs{
    /*弹性布局,在一行显示*/
    display: flex;
    padding: 30rpx 0;
    background-color: #fff;
    /*对齐方式,居中*/
    align-items: center;
    height: 126rpx;
    /*盒模型的控制,限制大小*/
    box-sizing: border-box;
}

.tabs-item{
    /*tab item均分父类空间*/
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.tab-label{
    color: #888888;
    /*不换行*/
    white-space: nowrap;
}

.active-tab{
    color: #333333;
    font-weight: bold;
}

.divider{
    height: 6rpx;
    width: 50rpx;
    margin-top: 10rpx;
    background-color: #f3d066;
}

js中函数

Page({
    data: {
        tabs: ['全部服务', '在提供', '正在找'],
        currentTabIndex: 0 //默认选中
    },
    onLoad: function (options) {

    },
    handleTabChange: function (event) {
        console.log(event)
        const index = event.currentTarget.dataset.index
        this.setData({
            currentTabIndex: index
        })
    }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值