微信小程序自定义tabbar

实现效果:

官方文档:

自定义 tabBar | 微信开放文档

实现步骤:

1. 在 app.json 中的 tabBar 项指定 custom 字段,同时其余 tabBar 相关配置也补充完整。

// 需要先定义tabBar页面
// “pages” 配置里面也不要忘了
"tabBar": {
    "custom": true,
    "color": "#999999",
    "selectedColor": "#eb7209",
    "backgroundColor": "#ffffff",
    "list": [
        {
            "pagePath": "pages/home/index",
            "iconPath": "static/images/tabbar/home.png",
            "selectedIconPath": "static/images/tabbar/home_cur.png",
            "text": "首页"
        },
        {
            "pagePath": "pages/bill/index",
            "iconPath": "static/images/tabbar/bill.png",
            "selectedIconPath": "static/images/tabbar/bill_cur.png",
            "text": "单据"
        },
        {
            "pagePath": "pages/plan/index",
            "iconPath": "static/images/tabbar/plan.png",
            "selectedIconPath": "static/images/tabbar/plan_cur.png",
            "text": "计划"
        },
        {
            "pagePath": "pages/mine/index",
            "iconPath": "static/images/tabbar/mine.png",
            "selectedIconPath": "static/images/tabbar/mine_cur.png",
            "text": "我的"
        }
    ]
},

2. 在根目录新建custom-tab-bar文件夹(切记,文件夹名称不能更改,不然获取不到),

#index.html

<!--custom-tab-bar/index.wxml-->
<cover-view class="custom-tabbar">
    <cover-view wx:for="{{tabbarList}}" wx:key="index" class="tabbar-item" data-index="{{index}}" bindtap="swicthTabbar">
        <cover-image src="{{currentName == item.name ? item.selectedIconPath : item.iconPath}}" class="tabbar-icon" style="{{ item.style }}"></cover-image>
        <cover-view class="{{currentName ==  item.name ? 'text-active' : 'tabbar-text'}}">{{ item.text }}</cover-view>
    </cover-view>
</cover-view>

#index.wxss

/* custom-tab-bar/index.wxss */
.custom-tabbar{
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 112rpx;
    background-color: #fff;
    box-shadow: 0px 6px 12px rgba(0, 0, 0, 0.24);
    /*(兼容有安全区域的手机)*/
    padding-bottom: constant(safe-area-inset-bottom);/*兼容 IOS<11.2*/
    padding-bottom: env(safe-area-inset-bottom);/*兼容 IOS>11.2*/
    display: flex;
}
.tabbar-item{
    flex: 1;
    height: 100%;
    display: flex;
    justify-content:center;
    align-items: center;
    flex-direction: column;
    font-size: 20rpx;
}
.tabbar-icon{
    width: 40rpx;
    height: 40rpx;
    margin-bottom: 8rpx;
}
.tabbar-text{
    color: #999999;
}
.text-active{
    color: #EB7209;
}

#index.js

// custom-tab-bar/index.js
Component({
    data:{
        currentName: 'home',
        //准备好自定义tabbar资源
        //自定义的tabBar 需要在app.json配置中存在
        tabbarList: [
            {
                "pagePath": "/pages/home/index",
                "iconPath": "/static/images/tabbar/home.png",
                "selectedIconPath": "/static/images/tabbar/home_cur.png",
                "text": "首页",
                "name": "home"
            },
            {
                "pagePath": "/pages/bill/index",
                "iconPath": "/static/images/tabbar/bill.png",
                "selectedIconPath": "/static/images/tabbar/bill_cur.png",
                "text": "单据",
                "name": "bill"
            },
            {
                "pagePath": "/pages/plan/index",
                "iconPath": "/static/images/tabbar/plan.png",
                "selectedIconPath": "/static/images/tabbar/plan_cur.png",
                "text": "计划",
                "name": "plan"
            },
            {
                "pagePath": "/pages/mine/index",
                "iconPath": "/static/images/tabbar/mine.png",
                "selectedIconPath": "/static/images/tabbar/mine_cur.png",
                "text": "我的",
                "name": "mine"
            }
        ]
    },
    lifetimes:{
      //组件生命周期
      attached(){
        //在组件实列进入页面节点树时执行
        console.log('嘿,我被执行了');
      }
    },
    methods:{
      //组件定义的方法(浓浓的既视感)
      swicthTabbar(e){
        let { index} = e.currentTarget.dataset;
        wx.switchTab({
          url: this.data.tabbarList[index].pagePath,
        })
      }
    }
})

 3.在app.js中定义全局setTabbar方法

App({
  //定义全局setTabbar方法
  $setTabbar(that, currentName){
    let tabbar = that.getTabBar();
    console.log(tabbar);
    tabbar.setData({
      currentName
    })
  },
  onLaunch() {
  },
})

4.在用到tabbar的页面添加更改当前选中页面方法(例如:home.js,bill.js,plan.js,mine.js)

onShow() {
    //'home' 根据页面来 在下面的tabbarList的name属性需要用到
    getApp().$setTabbar(this, 'home');
},

5.另外需要用到tabbar的页面需要考虑tabbar的高度(我是这样做处理的,自定义一个占位符)

<view class="plan">
    <!-- 内容 -->
    <view class="content"></view>
    <!-- 占位符 -->
    <view class="plan-block"></view>
</view>
.plan {
    height: 100%;
    padding: 0 40rpx;
    display: flex;
    flex-direction: column;
}
.plan .plan-block {
    box-sizing: content-box;
    height: 112rpx;
    /*(兼容有安全区域的手机)*/
    padding-bottom: constant(safe-area-inset-bottom);/*兼容 IOS<11.2*/
    padding-bottom: env(safe-area-inset-bottom);/*兼容 IOS>11.2*/
}
.content {
    overflow: hidden;
    flex: 1;
}

至此,微信小程序的自定义tabbar就完成了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

inticaler

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

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

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

打赏作者

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

抵扣说明:

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

余额充值