Tabbar组件必须放在小程序根目录下
文件夹名:custom-tab-bar
文件名都是:index
index.js
Component({
data: {
selected: 0,
color: "#333",
selectedColor: "#D70010",
list: [{
pagePath: "/pages/index/index",
iconPath: "/images/tabbar/shouye.png",
selectedIconPath: "/images/tabbar/shouye_active.png",
text: "主页"
}, {
pagePath: "/pages/categoryList/index",
iconPath: "/images/tabbar/fenlei.png",
selectedIconPath: "/images/tabbar/fenlei_active.png",
text: "分类"
}, {
pagePath: "/pages/unioncart/index",
iconPath: "/images/tabbar/gouwuche.png",
selectedIconPath: "/images/tabbar/gouwuche_active.png",
text: "购物车"
},{
iconPath: "/images/tabbar/kefu.png",
selectedIconPath: "/images/tabbar/kefu_active.png",
text: "客服"
}, {
pagePath: "/pages/my/index",
iconPath: "/images/tabbar/wode.png",
selectedIconPath: "/images/tabbar/wode_active.png",
text: "我的"
}]
},
attached() {
},
methods: {
switchTab(e) {
const data = e.currentTarget.dataset
const url = data.path
wx.switchTab({url})
this.setData({
selected: data.index
})
}
}
})
json
{
“component”: true
}
wxml
<block wx:if="{{index!=3}}">
<cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
<button class="tab-bar-item cs_button" open-type='contact'>
<cover-image style="text-align: center;margin-left: 6rpx;" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
</button>
wxss
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 100rpx;
background: white;
display: flex;
border-top: 1px solid #f1f1f1;
padding-bottom: env(safe-area-inset-bottom);
}
.cs_button{
background: #fff;
line-height: 38rpx;
}
.cs_button:after {
border: 0px;
}
.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 cover-image {
width: 48rpx;
height: 48rpx;
}
.tab-bar-item cover-view {
font-size: 30rpx;
}
app.json配置
“tabBar”: {
"custom": true,
"color": "#7A7E83",
"selectedColor": "#3cc51f",
"borderStyle": "black",
"backgroundColor": "#ffffff",
"list": [
{
"pagePath": "pages/index/index",
"iconPath": "images/tabbar/shouye.png",
"selectedIconPath": "images/tabbar/shouye_active.png",
"text": "主页"
}, {
"pagePath": "pages/categoryList/index",
"iconPath": "images/tabbar/fenlei.png",
"selectedIconPath": "images/tabbar/fenlei_active.png",
"text": "分类"
}, {
"pagePath": "pages/unioncart/index",
"iconPath": "images/tabbar/gouwuche.png",
"selectedIconPath": "images/tabbar/gouwuche_active.png",
"text": "购物车"
}, {
"pagePath": "pages/my/index",
"iconPath": "images/tabbar/wode.png",
"selectedIconPath": "images/tabbar/wode_active.png",
"text": "我的"
}
]
},
在页面引入菜单组件
在js中增加
onShow: function() {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: 1
})
}
},