微信小程序自定义tabbar、navbar
"tabBar": {
"custom": true,
...
}
<!--custom-tab-bar/index.wxml-->
<view class="tab-bar">
<view class="tab-bar-border"></view>
<view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
<image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></image>
<view class="text {{selected === index ? 'active' : 'normal'}}">{{item.text}}</view>
</view>
</view>
// custom-tab-bar/index.js
// 用自定义组件的方式编写即可,该自定义组件完全接管 tabBar 的渲染。另外,自定义组件新增 getTabBar 接口,可获取当前页面下的自定义 tabBar 组件实例。
Component({
data: {
selected: 0,
color: "#7A7E83",
selectedColor: "#3cc51f",
list : [
{
"pagePath": "/pages/index/index",
"text": "首页",
"iconPath": "/assets/imgs/shouye_1.png",
"selectedIconPath": "/assets/imgs/index-on.png"
},
{
"pagePath": "/pages/cates/cates",
"text": "分类",
"iconPath": "/assets/imgs/fenlei.png",
"selectedIconPath": "/assets/imgs/cates-on.png"
},
{
"pagePath": "/pages/search/search",
"text": "搜索门店",
"iconPath": "/assets/imgs/dingwei.png",
"selectedIconPath": "/assets/imgs/dingwei-on.png"
},
{
"pagePath": "/pages/cart/cart",
"text": "购物车",
"iconPath": "/assets/imgs/chengxu.png",
"selectedIconPath": "/assets/imgs/cart-on.png"
},
{
"pagePath": "/pages/home/home",
"text": "我的",
"iconPath": "/assets/imgs/zuanshi.png",
"selectedIconPath": "/assets/imgs/home-on.png"
}
]
},
methods: {
// 这里是一个自定义方法
switchTab(e) {
const data = e.currentTarget.dataset
const url = data.path
wx.switchTab({url})
// this.setData({
// selected: data.index
// })
},
},
})
{
"component": true
}
.active{
color: #FF6601;
}
.tab-bar {
position: fixed;
z-index: 999;
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 image {
width: 27px;
height: 27px;
}
.tab-bar-item view {
font-size: 10px;
}
注:在tab页面的onshow方法内,定义以下 解决路径跳转图标不跳转问题
onShow: function () {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: 3
})
}
}
// navbar
<view class="navbar" style="height: {{navHeight}}px;">
<view class="nav" style="top: {{navTop}}px;">
<image src="../../assets/imgs/icon.png"></image>
<view class="ipt">
<input type="text" placeholder="搜索商品" style="background: url(../../assets/imgs/sousuo_o.png) no-repeat 12rpx center;background-size: 40rpx 40rpx;"/>
</view>
</view>
</view>
// app.js 获取导航栏高度
onLaunch() {
let menuButtonObject = wx.getMenuButtonBoundingClientRect();
wx.getSystemInfo({
success: res => {
let statusBarHeight = res.statusBarHeight,
navTop = menuButtonObject.top,//胶囊按钮与顶部的距离
navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight)*2;//导航高度
this.globalData.navHeight = navHeight;
this.globalData.navTop = navTop;
},
fail(err) {
console.log(err);
}
})
},
.navbar {
width: 100%;
/* height: 84rpx; */
overflow: hidden;
position: fixed;
background-color: #FF5700;
top: 0;
left: 0;
z-index: 10;
flex-shrink: 0;
color: #fff;
}
.nav{
z-index: 99999;
width: 100%;
position: absolute;
left: 0;
display: flex;
justify-content: flex-start;
align-items: center;
}
...