快速掌握微信小程序 tab 切换的实现技巧(可滑动切换)

本文介绍了如何在微信小程序中实现Tab切换功能,通过控制选项的样式和内容显示,结合点击和滑动事件,为用户提供便捷的交互体验。示例代码包括WXML、JS和WXSS文件,展示了两种不同风格的Tab切换效果。

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

前言

微信小程序中的 tab 切换功能可以说是用户所需的一个基础功能。本文将介绍如何通过微信小程序实现 tab 切换功能,为用户带来更为便捷和高效的小程序体验。


实现思路

其实这个小功能的实现非常简单,只需要通过一个标识控制选项的样式及显示的内容,当我们触发点击或者滑动事件时动态的改变标识的值即可。话不多说,下面直接上实例代码。


效果1:

在这里插入图片描述


wxml 文件

<view>
    <!-- Tab布局 -->
    <view class="navBox">
        <view class="titleBox" wx:for="{{tabList}}" bindtap="tabsOn" data-idx="{{item.index}}">
            <text class="{{item.index == tabsId ? 'fontColorBox' : ''}}">{{item.title}}</text>
            <hr class="{{item.index == tabsId ? 'lineBox' : ''}}" />
        </view>
    </view>
    <!-- 内容布局 -->
    <swiper class="swiperTtemBox" bindchange="slideOn" current="{{tabsId}}" circular>
        <!-- circular 启用循环滑动 -->
        <swiper-item>
            <view>装备内容</view>
        </swiper-item>
        <swiper-item>
            <view>活动内容</view>
        </swiper-item>
        <swiper-item>
            <view>功能内容</view>
        </swiper-item>
    </swiper>
</view>

js文件

Page({
    data: {
        // tab选项
        tabList: [
            {title: "装备",index: "0",},
            {title: "运动",index: "1",},
            {title: "功能",index: "2",}
        ],
        tabsId: 0, //默认选型为装备
    },
    // 滑动时触发的事件
    slideOn(e) {
        // 拿到当前索引并动态改变
        this.setData({
            tabsId: e.detail.current
        })
    },

    //点击tab时触发
    tabsOn(e) {
        this.setData({
            //拿到当前索引并动态改变
            tabsId: e.currentTarget.dataset.idx
        })
    },
})

wxss 文件

.navBox {
  /* tab整体样式 */
  height: 100rpx;
  padding: 0px 20%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  border-bottom: 18rpx solid rgb(243, 244, 249);
}

.fontColorBox {
  /* 选中tab样式 */
  color: black;
  font-weight: bold;
}

.titleBox {
  /* 未选中tab样式 */
  color: rgb(168, 170, 175);
  font-size: 28rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.lineBox {
  /* 线条样式 */
  width: 32rpx;
  height: 8rpx;
  background: black;
  margin-top: 10rpx;
  border-radius: 4rpx;
}

.swiperTtemBox {
  /* 内容样式 */
  padding: 16rpx;
  font-size: 28rpx;
  height: calc(100vh - 150rpx);
}

效果2:

在这里插入图片描述


wxml 文件

<view class="tabBox">
    <!-- Tab布局 -->
    <view class="navBox">
        <view class="titleBox" wx:for="{{tabList}}" bindtap="tabsOn" data-idx="{{item.index}}">
            <text class="{{item.index == tabsId ? 'fontColorBox' : ''}}">{{item.title}}</text>
        </view>
    </view>
    <!-- 内容布局 -->
    <swiper class="swiperTtemBox" bindchange="slideOn" current="{{tabsId}}" circular>
        <!-- circular 启用循环滑动 -->
        <swiper-item>
            <view>装备内容</view>
        </swiper-item>
        <swiper-item>
            <view>活动内容</view>
        </swiper-item>
        <swiper-item>
            <view>功能内容</view>
        </swiper-item>
    </swiper>
</view>

wxss 文件

.tabBox {
    padding: 20rpx;
}

.navBox {
    /* tab整体样式 */
    height: 74rpx;
    display: flex;
    padding: 1.5% 1.5%;
    border-radius: 50rpx;
    background: #E5EEFD;
}

.fontColorBox,
.titleBox {
    /* 共同样式 */
    height: 100%;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.fontColorBox {
    /* 选中tab样式 */
    color: #fff;
    font-weight: bold;
    background: linear-gradient(151deg, #2F7EFC 0%, #7BADFC 100%);
    border-radius: 50rpx;
}

.titleBox {
    /* 未选中tab样式 */
    color: #1A1A1A;
    font-size: 28rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.swiperTtemBox {
    /* 内容样式 */
    padding: 16rpx;
    font-size: 28rpx;
    height: calc(100vh - 150rpx);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水星记_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值