# wechat-app-tabbar **Repository Path**: Alimjan2009/wechat-app-tabbar ## Basic Information - **Project Name**: wechat-app-tabbar - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-02-07 - **Last Updated**: 2021-02-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # wechat-app-tabbar ## 微信小程序 自定义tabbar [项目地址,欢迎 star](https://github.com/songzeng2016/wechat-app-tabbar) [https://github.com/songzeng2016/wechat-app-tabbar](https://github.com/songzeng2016/wechat-app-tabbar) ### 前言 项目中我们可能会用到两层 tabbar 而小程序只能配置出一层,或者我们想自定义 tabbar 的样式或功能,这时候就需要我们自己定义 tabbar。 先来张图看下效果,下面是实现步骤。 ![tabbar](https://github.com/songzeng2016/wechat-app-tabbar/raw/master/images/GIF.gif) ### 创建wxml模板 ```html ``` ### wxss布局 ```css .tabbar_box{ display: flex; flex-direction: row; justify-content: space-around; position: fixed; bottom: 0; left: 0; z-index: 999; width: 100%; height: 120rpx; border-top: 1rpx solid gray; } .tabbar_nav{ display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: 28rpx; height: 100%; } .tabbar_icon{ width: 61rpx; height: 61rpx; } ``` 布局不是重点也可以自定义布局也可以引用我写好的样式 ### 重点来了 tabbar的参数配置 ```javascript tabbar:{ color: "#000000", selectedColor: "#0f87ff", backgroundColor: "#ffffff", borderStyle: "black", list: [ { pagePath: "/pages/tabbar/tabbar", text: "项目", iconPath: "/images/item.png", selectedIconPath: "/images/item_HL.png", selected: true }, { pagePath: "/pages/address/address", text: "通讯录", iconPath: "/images/ts.png", selectedIconPath: "/images/ts1.png", selected: false }, { pagePath: "/pages/personal/personal", text: "文件", iconPath: "/images/wjj.png", selectedIconPath: "/images/wjj1.png", selected: false } ], position: "bottom" } ``` 有没有感觉很熟悉,没错就是你熟悉的tababar配置,仅仅增加了一个selected参数来表示选中的状态 另外一点要注意的是我们的tabbar数据配置在app.js里面而不是app.json里面 ### 最后还有一个比较重要的点 在app.js里面的一个函数 ```javascript editTabBar: function(){ var tabbar = this.globalData.tabbar, currentPages = getCurrentPages(), _this = currentPages[currentPages.length - 1], pagePath = _this.__route__; (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath); for(var i in tabbar.list){ tabbar.list[i].selected = false; (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true); } _this.setData({ tabbar: tabbar }); }, ``` ### 我们完整的app.js是这样的 ```javascript //app.js App({ onLaunch: function () { //调用API从本地缓存中获取数据 var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) }, getUserInfo:function(cb){ var that = this if(this.globalData.userInfo){ typeof cb == "function" && cb(this.globalData.userInfo) }else{ //调用登录接口 wx.login({ success: function () { wx.getUserInfo({ success: function (res) { that.globalData.userInfo = res.userInfo typeof cb == "function" && cb(that.globalData.userInfo) } }) } }) } }, editTabBar: function(){ var tabbar = this.globalData.tabbar, currentPages = getCurrentPages(), _this = currentPages[currentPages.length - 1], pagePath = _this.__route__; (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath); for(var i in tabbar.list){ tabbar.list[i].selected = false; (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true); } _this.setData({ tabbar: tabbar }); }, globalData:{ userInfo:null, tabbar:{ color: "#000000", selectedColor: "#0f87ff", backgroundColor: "#ffffff", borderStyle: "black", list: [ { pagePath: "/pages/tabbar/tabbar", text: "项目", iconPath: "/images/item.png", selectedIconPath: "/images/item_HL.png", selected: true }, { pagePath: "/pages/address/address", text: "通讯录", iconPath: "/images/ts.png", selectedIconPath: "/images/ts1.png", selected: false }, { pagePath: "/pages/personal/personal", text: "文件", iconPath: "/images/wjj.png", selectedIconPath: "/images/wjj1.png", selected: false } ], position: "bottom" } } }) ``` 生成的东西我没有删掉 ### 到这准备工作已经完成  下面就是怎么使用 #### 在wxml引入创建的模板并使用 ```html