1、首先 app.json文件 找到 tabBar 添加 “custom”: true 开启自定义tabBar
2、根目录下新建custom-tab-bar目录 新建index(Component组件)**必须是index命名
2.1 wxml 文件
<cover-view class="tab-bar">
<cover-view class="tab-bar-border"></cover-view>
<cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
<cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view style="color: {{selected === index ? selectedColor : color}};font-size:28rpx;margin-top:6rpx;">{{item.text}}</cover-view>
</cover-view>
</cover-view>
2.2 wxss文件
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
padding-top: 20rpx;
background: white;
display: flex;
padding-bottom: env(safe-area-inset-bottom);
}
.tab-bar-border {
background-color: rgba(0, 0, 0, 0.33);
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 2rpx;
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 cover-image {
width: 54rpx;
height: 54rpx;
margin-bottom: 10rpx;
}
.tab-bar-item cover-view {
font-size: 20rpx;
}
2.3 js文件
Component({
data: {
selected: 0,
color: "#7A7E83",
selectedColor: "#cf000e",
"list": [
{
"pagePath": "/pages/index/index",
"text": "首页",
"iconPath": "/img/home.png",
"selectedIconPath": "/img/home_on.png"
},
{
"pagePath": "/pages/category/category",
"text": "分类",
"iconPath": "/img/sort.png",
"selectedIconPath": "/img/sort_on.png"
},
{
"pagePath": "/pages/shoppingcart/tabBarCart",
"text": "购物车",
"iconPath": "/img/cart.png",
"selectedIconPath": "/img/cart_on.png"
},
{
"pagePath": "/pages/mine/mine",
"text": "我的",
"iconPath": "/img/my.png",
"selectedIconPath": "/img/my_on.png"
}
]
},
onshow() {
console.log('onshow.....')
},
methods: {
switchTab(e) {
const data = e.currentTarget.dataset
console.log(data)
const url = data.path
wx.switchTab({ url })
console.log(e)
this.setData({
selected: data.index
})
}
}
})
2.4 json文件
{
"component": true
}
3.使用~