自定义tabbar组件
<!--miniprogram/custom-tab-bar/index.wxml-->
<cover-view class="tab-bar">
<cover-view wx:for="{{ list }}" wx:key="index" class="tab-bar-item" data-path="{{ item.pagePath }}" data-index="{{ index }}" bindtap="switchTab">
<cover-image class="img" src="{{ selected === index ? item.selectedIconPath : item.iconPath }}"></cover-image>
<!-- <cover-view style="color: {{ selected === index ? selectedColor : color }}">{{ item.text }}</cover-view> -->
</cover-view>
</cover-view>
Component({
data: {
selected: 0,
color: '#7A7E83',
selectedColor: '#3cc51f',
list: [
{
pagePath: '/pages/home/index',
iconPath: '/images/tabbar/ic_home_active.png',
selectedIconPath: '/images/tabbar/ic_home.png'
},
{
pagePath: '/pages/Profile/index',
iconPath: '/images/tabbar/ic_my.png',
selectedIconPath: '/images/tabbar/ic_my_active.png'
}
]
},
attached() {},
methods: {
switchTab(e) {
const data = e.currentTarget.dataset;
const url = data.path;
wx.switchTab({ url });
this.setData({
selected: data.index
});
}
}
});
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 50px;
background: white;
display: flex;
padding-bottom: env(safe-area-inset-bottom);
box-shadow: 0px -2px 4px 0px rgba(0, 0, 0, 0.08);
}
/* .tab-bar-border {
background-color: rgba(0, 0, 0, 0.33);
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 1px;
transform: scaleY(0.5);
} */
.tab-bar-item {
flex: 1;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.tab-bar-item .img {
width: 44rpx;
height: 44rpx;
}
.tab-bar-item:nth-child(2) .img {
width: 44rpx;
height: 48rpx;
}
/* .tab-bar-item cover-view {
font-size: 10px;
} */
appjson
"tabBar": {
"custom": true,
"color": "#7A7E83",
"selectedColor": "#3cc51f",
"borderStyle": "black",
"backgroundColor": "#ffffff",
"list": [
{
"pagePath": "pages/home/index",
"iconPath": "/images/tabbar/ic_home_active.png",
"selectedIconPath": "/images/tabbar/ic_home.png"
},
{
"pagePath": "pages/Profile/index",
"iconPath": "/images/tabbar/ic_my.png",
"selectedIconPath": "/images/tabbar/ic_my_active.png"
}
]
},
使用
Component({
pageLifetimes: {
show() {
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 1
});
}
}
},
})