微信小程序 Tab实现,可滑动、可点击

本文介绍了一个小程序中实现Tab栏切换的示例代码。通过wxml和wxss定义了三个状态(全部、中奖、未中奖)的Tab项,并使用js文件中的逻辑进行状态切换,包括滑动切换和点击切换。

效果:

这里写图片描述

直接码吧,做个笔记。

tab.wxml:

<view class="swiper-tab">
    <view class="swiper-tab-item {{currentTab==0?'active':''}}" data-current="0" bindtap="clickTab">全部</view>
    <view class="swiper-tab-item {{currentTab==1?'active':''}}" data-current="1" bindtap="clickTab">中奖</view>
    <view class="swiper-tab-item {{currentTab==2?'active':''}}" data-current="2" bindtap="clickTab">未中奖</view>
</view>

<swiper current="{{currentTab}}" duration="300" bindchange="swiperTab">
    <swiper-item>
        <view>全部</view>
    </swiper-item>
    <swiper-item>
        <view>中奖</view>
    </swiper-item>
    <swiper-item>
        <view>未中奖</view>
    </swiper-item>
</swiper>

tab.wxss:

.swiper-tab {
    width: 100%;
    border-bottom: 2rpx solid #ccc;
    text-align: center;
    height: 88rpx;
    line-height: 88rpx;
    display: flex;
    flex-flow: row;
    justify-content: space-between;
}

.swiper-tab-item {
    width: 30%;
    color: #434343;
}

.active {
    color: #f65959;
    border-bottom: 4rpx solid #f65959;
}

swiper {
    text-align: center;
    background-color: #fff
}

tab.js:

Page({
    data: {
        currentTab: 0
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
    },

    //滑动切换
    swiperTab: function (e) {

        this.setData({
            currentTab: e.detail.current
        }); 
    },

    //点击切换
    clickTab: function (e) {
        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            this.setData({
                currentTab: e.target.dataset.current
            })
        }
    }
})
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值