0. 文档位置
自定义tabbarhttps://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html
1. 使用
1.1 随便创建一个文件夹---创建一个commponent
1.2 html
<!--miniprogram/custom-tab-bar/index.wxml-->
<cover-view class="tab-bar">
<cover-view class="tab-bar-border"></cover-view>
<!-- 循环列表,获取路由及index -->
<cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
<block wx:if="{{ index === 2 }}" >
<cover-view class="pub">{{item.text}}</cover-view>
</block>
<block wx:else>
<cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
</block>
</cover-view>
</cover-view>
<!--选中的索引,根据tabbar中js配的菜单位置,自己调整-->
<!-- <tabber selected="{{ 3 }}"></tabber> -->
1.3 css
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 48px;
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: 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: 27px;
height: 27px;
}
.tab-bar-item cover-view {
font-size: 10px;
}
.pub {
background-color: #fa3d;
height: 80rpx;
width: 80rpx;
border-radius: 50%;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
1.4 js
var app = getApp();
Component({
properties: {
selected: {
type: Number, // html选中找两个地方,一个data,一个属性 全局就能用
value: 0
}
},
data: {
color: "#7A7E83", // 颜色
selectedColor: "#FF0000", // 被选中颜色
list: [{
pagePath: "/pages/index/index",
iconPath: "/static/tabbar/no.png",
selectedIconPath: "/static/tabbar/yes.png",
text: "首页"
},
{
pagePath: "/pages/auction/auction",
text: "拍卖",
iconPath: "/static/tabbar/no.png",
selectedIconPath: "/static/tabbar/yes.png"
},
{
text: "发布"
},
{
pagePath: "/pages/news/news",
text: "消息",
iconPath: "/static/tabbar/no.png",
selectedIconPath: "/static/tabbar/yes.png"
},
{
pagePath: "/pages/home/home",
iconPath: "/static/tabbar/no.png",
selectedIconPath: "/static/tabbar/yes.png",
text: "我的"
},
]
},
attached() {},
methods: {
switchTab(e) {
// console.log(e.currentTarget.dataset.path)
var url = e.currentTarget.dataset.path
// 根据index判断,发布是渲染的时候是没有url的
if (url) {
wx.switchTab({
url
})
} else {
if (app.globalData.userinfo) {
wx.navigateTo({
url: '/pages/release/release',
})
} else {
wx.navigateTo({
url: '/pages/login/login',
})
}
}
// this.setData({
// selected: data.index
// })
}
}
})
1.5 全局app.json配置
- 原先的tarbar页面,如果是在app.json中配置了,依旧是无法跳转的,需要在app.json去除,才能正常跳转
- custom不使用默认的tabbar,使用自定义的,一旦加上,默认的就会失效
"tabBar": {
"selectedColor": "#CD5C5C",
"custom": true,
"list": [
{
"pagePath": "pages/index/index",
"text": "首页",
"iconPath": "/static/tabbar/no.png",
"selectedIconPath": "/static/tabbar/yes.png"
},
{
"pagePath": "pages/auction/auction",
"text": "拍卖",
"iconPath": "/static/tabbar/no.png",
"selectedIconPath": "/static/tabbar/yes.png"
},
{
"pagePath": "pages/news/news",
"text": "消息",
"iconPath": "/static/tabbar/no.png",
"selectedIconPath": "/static/tabbar/yes.png"
},
{
"pagePath": "pages/home/home",
"text": "我的",
"iconPath": "/static/tabbar/no.png",
"selectedIconPath": "/static/tabbar/yes.png"
}
]
},
1.6 使用的界面配置(一般会在原tabbar的界面配置)
1.6.1 app.json
{
"usingComponents": {
"tabber": "/commponents/tabbar"
}
}
1.6.2 html(写在最后/最前) 这个页面是我的 索引为2
<!--选中的索引,根据自定义tabbar中js配的菜单位置,自己调整-->
<tabber selected="{{ 0 }}"></tabber>
2. 使用步骤
# 写一个组件
# app.json配置
# 使用
# 在使用的页面的json中配置
# 在原先的tabbar的页面上直接使用
# 导入select(选中变色)
# 原先的tarbar页面,如果是在app.json中配置了,是无法直接跳转的,需要在app.json去除,才可以正常跳转