app.json中
- 创建tabbar对应的路由页面
- 配置tabbar配置项,指定custom字段为true,配置list等,这里的list要和自定义tabbar里面配置的完全一致
app.json
{
"pages":[
"pages/index/index",
"pages/special/special",
"pages/shopping/shopping",
"pages/personal/personal"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "Weixin",
"navigationBarTextStyle":"black"
},
"tabBar": {
"custom": true,
"color": "#7A7E83",
"selectedColor": "#3cc51f",
"borderStyle": "black",
"backgroundColor": "#ffffff",
"list": [{
"pagePath": "pages/index/index",
"iconPath": "/static/image/home.png",
"selectedIconPath": "/static/image/home_select.png",
"text": "组件"
},
{
"pagePath": "pages/special/special",
"iconPath": "/static/image/like.png",
"selectedIconPath": "/static/image/like-active.png",
"text": "我的特卖"
},
{
"pagePath": "pages/shopping/shopping",
"iconPath": "/static/image/shopping.png",
"selectedIconPath": "/static/image/shopping-active.png",
"text": "购物车"
},
{
"pagePath": "pages/personal/personal",
"iconPath": "/static/image/admin.png",
"selectedIconPath": "/static/image/admin-active.png",
"text": "个人"
}
]
},
"usingComponents": {},
"subpackages": [{
"root": "orderDetails",
"pages": [
"orderDetails/orderDetails"
]
},{
"root": "complaint",
"pages": [
"complaint/complaint"
]
},{
"root": "changeDetail",
"pages": [
"changeDetail/changeDetail"
]
}],
"style": "v2",
"sitemapLocation": "sitemap.json"
}
创建自定义tabbar
- 根目录下创建components文件夹
- components内新建bottom-tab-bar文件夹,右键新建components
- bottom-tab-bar.js中配置selected,color,selectedColor,list等字段。methods里面配置switchTab为点击tabbar切换的回调拿到跳转的地址,用wx.switchTab进行路由跳转。创建run方法,这个方法是跳转到目标地址的时候目标地址通过获取这个tabbar实例然后调用run方法进行改变tabbar的高亮效果。
bottom-tab-bar.js
Component({
data: {
selected: null,
color: "#7A7E83",
selectedColor: "#3cc51f",
"list": [{
"pagePath": "/pages/index/index",
"iconPath": "https://chirse.oss-cn-beijing.aliyuncs.com/tabbar_img/home.png",
"selectedIconPath": "https://chirse.oss-cn-beijing.aliyuncs.com/tabbar_img/home_active.png",
"text": "首页"
},
{
"pagePath": "/pages/special/special",
"iconPath": "https://chirse.oss-cn-beijing.aliyuncs.com/tabbar_img/beer.png",
"selectedIconPath": "https://chirse.oss-cn-beijing.aliyuncs.com/tabbar_img/beer_active.png",
"text": "官网"
},
{
"pagePath": "/pages/shopping/shopping",
"iconPath": "https://chirse.oss-cn-beijing.aliyuncs.com/tabbar_img/shopping.png",
"selectedIconPath": "https://chirse.oss-cn-beijing.aliyuncs.com/tabbar_img/shopping_active.png",
"text": "购物车"
},
{
"pagePath": "/pages/personal/personal",
"iconPath": "https://chirse.oss-cn-beijing.aliyuncs.com/tabbar_img/me.png",
"selectedIconPath": "https://chirse.oss-cn-beijing.aliyuncs.com/tabbar_img/me_active.png",
"text": "购物车"
},
]
},
attached() {},
methods: {
switchTab(e) {
const data = e.currentTarget.dataset
const url = data.path
wx.switchTab({
url
})
},
run(index) {
console.log(this.data.active);
this.setData({
selected:index
});
console.log(this.data.selected);
}
}
})
bottom-tab-bar.json
{
"component": true
}
bottom-tab-bar.wxml
<!--miniprogram/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">
<cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<view style="color: {{selected === index ? selectedColor : color}};">{{item.text}}</view>
</view>
<view class="one" style="display: {{selected ===0?'block':'none'}};">
<cover-image src="/static/image/home_select.png"></cover-image>
</view>
</view>
bottom-tab-bar.wxss
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
width: 100%;
height: 48px;
background: white;
display: flex;
padding-bottom: env(safe-area-inset-bottom);
z-index: 50;
}
.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 view {
font-size: 10px;
}
.one {
width: 70rpx;
height: 70rpx;
position: absolute;
left: 40rpx;
top: 7rpx;
z-index: 10;
}
.one>cover-image {
display: none;
width: 70rpx;
height: 70rpx;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.two {
width: 70rpx;
height: 70rpx;
position: absolute;
left: 40rpx;
top: 67rpx;
z-index: 5;
background-color: #fff;
}
页面组件部分(以index页面为例)
- 首先在index.json里面指定usingComponents配置项引入自定义tabbar组件
- 然后在index.wxml里面书写自定义tabbar组件,然后指定id为tabbar
- index.js里面配置data配置项tabBarIndex为当前路由组件对应的索引
- 然后再onLoad钩子中利用selectComponent获取自定义tabbar的实例,调用它的run方法,传递保存在当前组件里的tabBarIndex,然后由run改变自定义tabbar的高亮状态
index.json
{
"usingComponents": {
"TabBar": "/components/bottom-tab-bar/bottom-tab-bar"
}
}
index.wxml
<!--index.wxml-->
<view class="container">
<TabBar id="tabbar"></TabBar>
<view class="userinfo">
<block wx:if="{{canIUseOpenData}}">
<view class="userinfo-avatar" bindtap="bindViewTap">
<open-data type="userAvatarUrl"></open-data>
</view>
<open-data type="userNickName"></open-data>
</block>
<block wx:elif="{{!hasUserInfo}}">
<button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
<button wx:elif="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
<view wx:else> 请使用1.4.4及以上版本基础库 </view>
</block>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto">
<text class="user-motto">{{motto}}</text>
</view>
</view>
index.js
const app = getApp()
Page({
data: {
tabBarIndex: 0,
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
canIUseGetUserProfile: false,
canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName')
},
bindViewTap() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad() {
let tabbar = this.selectComponent('#tabbar')
tabbar.run(this.data.tabBarIndex)
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
},
onShow() {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: null
})
}
},
getUserProfile(e) {
wx.getUserProfile({
desc: '展示用户信息',
success: (res) => {
console.log(res)
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
},
getUserInfo(e) {
console.log(e)
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
}
})
index.wxss
.banners {
width: 100%;
height: 300rpx;
}
.banners image {
width: 100%;
height: 100%;
}
.navContainer {
display: flex;
}
.navItem {
display: flex;
flex-direction: column;
align-items: center;
width: 20%;
}
.navItem .iconfont {
width: 100rpx;
height: 100rpx;
border-radius: 50%;
text-align: center;
line-height: 100rpx;
background: rgb(240, 19, 19);
font-size: 50rpx;
color: #fff;
margin: 20rpx 0;
}
.navItem text {
font-size: 26rpx;
}
.recommendContainer {
padding: 20rpx;
}
.recommendScroll {
display: flex;
height: 300rpx;
}
.scrollItem {
width: 200rpx;
margin-right: 20rpx;
}
.scrollItem image {
width: 200rpx;
height: 200rpx;
border-radius: 10rpx;
}
.scrollItem text {
font-size: 26rpx;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
.topList {
padding: 20rpx;
}
.topListSwiper {
height: 400rpx;
}
.swiperItem {
width: 96%;
background: #fbfbfb;
}
.swiperItem .title {
font-size: 30rpx;
line-height: 80rpx;
}
.musicItem {
display: flex;
margin-bottom: 20rpx;
}
.musicItem image{
width: 100rpx;
height: 100rpx;
border-radius: 6rpx;
}
.musicItem .count {
width: 100rpx;
height: 100rpx;
text-align: center;
line-height: 100rpx;
}
.musicItem .musicName {
height: 100rpx;
line-height: 100rpx;
max-width: 400rpx;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}