app.json
"tabBar": {
"custom": true,
"list": [
{
"pagePath": "pages/index/index"
},
{
"pagePath": "pages/goods/index"
},
{
"pagePath": "pages/add/index"
},
{
"pagePath": "pages/shop/index"
},
{
"pagePath": "pages/my/index"
}
]
}
app.js
App({
$setTabbar(that,currentName){
if(typeof that.getTabBar == 'function' && that.getTabBar()){
let tabbar = that.getTabBar();
console.log(tabbar);
tabbar.setData({
currentName
})
}
},
onLaunch() {
},
globalData: {
}
})
add.wxml,shop.wxml,index.wxml,goods.wxml,my.wxml
Page({
data: {
},
onShow(){
getApp().$setTabbar(this,'index');
}
})
核心:custom-tab-bar(需要在根目录下且名字不能变)
# index.js
Component({
data:{
currentName:'index',
tabbarList:[{
"pagePath": "/pages/index/index",
"text": "index",
"iconPath": "../assets/tabbar/1-1.png",
"selectedIconPath": "../assets/tabbar/1.png",
"style":'',
"name":'index'
},
{
"pagePath": "/pages/goods/index",
"text": "goods",
"iconPath": "../assets/tabbar/2-2.png",
"selectedIconPath": "../assets/tabbar/2.png",
"style":'',
"name":'goods'
},
{
"pagePath": "/pages/add/index",
"text": "",
"iconPath": "../assets/tabbar/addCase.png",
"selectedIconPath": "../assets/tabbar/addCase.png",
"style":'width:96rpx;height:70rpx',
"name":'add'
},
{
"pagePath": "/pages/shop/index",
"text": "shop",
"iconPath": "../assets/tabbar/3-3.png",
"selectedIconPath": "../assets/tabbar/3.png",
"style":'',
"name":'shop'
},
{
"pagePath": "/pages/my/index",
"text": "my",
"iconPath": "../assets/tabbar/4-4.png",
"selectedIconPath": "../assets/tabbar/4.png",
"style":'',
"name":'my'
}]
},
lifetimes:{
attached(){
console.log('嘿,我被执行了');
}
},
methods:{
swicthTabbar(e){
let { index} = e.currentTarget.dataset;
wx.switchTab({
url: this.data.tabbarList[index].pagePath,
})
}
}
})
# 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>
style="{{item.style}}" 这个可以会报{ expectedcss(css-lcurlyexpected),不必理会,好像是版本库的问题
index.wxss
.custom-tabbar{
position: fixed;
bottom: 0;
left: 0;
right: 0;
background-color: #fff;
box-shadow: 0px 0px 4rpx rgba(0, 0, 0, 0.3);
height: 96rpx;
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
display: flex;
}
.tabbar-item{
display: flex;
justify-content:center;
align-items: center;
flex: 1;
flex-direction: column;
font-size: 24rpx;
}
.tabbar-icon{
width: 44rpx;
height: 44rpx;
}
.tabbar-text{
color: #9c9c9c;
}
.text-active{
color: #000;
}
完整demo:https://gitee.com/azhe2000/noob/tree/master/applets-tabbar
效果图
|