前言
用过微信提供的tabBar
组件就会发现,局限性不少,样式上的局限性不说,tabBar
是写在app.json
配置文件中的,这就意味着不能动态改变tabBar
的个数,不能在tabBar
上面添加函数等等。
加上最近客户有需求,要求tabBar
动态改变个数,于是折腾许久写了一个,分享出来。
代码
wxml
wxml模板1
2
3
4
5
6
7
8
9
10
11
12<template name="tabBar">
<view class="tabBar">
<block wx:for="{{tabBar}}" wx:for-item="item" wx:key="tabBar">
<view class="tabBar-item">
<view bindtap="goTab" data-path="{{item.pagePath}}">
<view><image class="icon" src='{{item.iconPath}}'></image></view>
<view class="{{item.active== true ? 'tabBartext' :''}}">{{item.text}}</view>
</view>
</view>
</block>
</view>
</template>
wxss
wxss模板1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28.icon{
width:54rpx;
height: 54rpx;
}
.tabBar{
height: 90rpx;
width:100%;
position: fixed;
bottom:0;
left: 0;
padding:10rpx;
background:#F7F7FA;
font-size:20rpx;
color:#8A8A8A;
box-shadow: 6rpx 6rpx 6rpx 6rpx #aaa;
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
}
.tabBar-item{
text-align: center;
overflow: hidden;
width: 120rpx;
}
.tabBartext{
color:#1296DB;
}
tabbar数据
tabbar
的数据采用仿照官方原生tabbar
配置文件的写法,可以通过后台更改值实现tabbar
个数的动态变化。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42var tabbarData = [
{
"active": false,
"id":0,
"pagePath": "/pages/index/index",
"iconPath": "/imgs/icon/index.png",
"selectedIconPath": "/imgs/icon/index-s.png",
"text": "首页"
},
{
"active": false,
"id": 1,
"pagePath": "/pages/classify/classify",
"iconPath": "/imgs/icon/classify.png",
"selectedIconPath": "/imgs/icon/classify-s.png",
"text": "分类"
},
{
"active": false,
"id": 2,
"pagePath": "/pages/cart/cart",
"iconPath": "/imgs/icon/cart.png",
"selectedIconPath": "/imgs/icon/cart-s.png",
"text": "购物车"
},
{
"active": false,
"id": 3,
"pagePath": "/pages/store/store",
"iconPath": "/imgs/icon/store.png",
"selectedIconPath": "/imgs/icon/store-s.png",
"text": "门店"
},
{
"active": false,
"id": 4,
"pagePath": "/pages/user/user",
"iconPath": "/imgs/icon/user.png",
"selectedIconPath": "/imgs/icon/user-s.png",
"text": "我的"
}
];
js
首先是公用方法,在utils.js
文件里添加下面的方法
1 | //tabbar的样式切换 |
使用
在要使用tabbar
的页面wxml
里引入以下代码1
2<import src="../template/tabbar/tabbar-template.wxml" />
<template is="tabBar" data="{{tabBar:bindData.tabBar}}"/>
wxss
里面引入1
@import "../template/tabbar/tabbar-template.wxss" ;
在要使用tabbar
的页面引入utils
方法1
var utils = require("../../utils/util.js");
首页的js
文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40var utils = require("../../utils/util.js");
Page({
data: {
},
onLoad: function (options) {
var tabbarData = "后台请求获取到上面的tabbar数据";
// 查询包含首页字符的页面id
function checkData(item) {
return item.text == "首页";
}
var tabbarId = tabbarData.find(checkData).id;
this.setData({
tabbarId: tabbarId
})
// 调用utils.tabbar方法,切换tabbar的样式。
utils.tabbar("tabBar", that.data.tabbarId, that, tabbarData);
//引用的数组因为切换样式时数据被修改,保存到本地存储时是被修改状态
//我们需要的是数据从后台传来时的初始状态,进行下列还原操作
//也可以对数组进行深度拷贝,对拷贝的数组进行存储操作
tabbarData[0].active = false;
tabbarData[0].iconPath = "/imgs/icon/index.png";
//避免每次加载tabbar页面都发送请求,首页加载成功后保存在本地存储。
wx.setStorageSync("tabbarData", tabbarData);
},
// Tabbar跳转
goTab:function(e){
utils.goTab(e, this, this.data.tabbarId)
}
})
其他要显示tabbar
页面的js
文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29var utils = require("../../utils/util.js");
Page({
data: {
},
onLoad: function (options) {
// 获取tabbar数组的初始状态
var tabbarData = wx.getStorageSync("tabbarData");
function checkData(item) {
return item.text == "分类";
//其他页面赋值右边的字符串输入对应的页面描述,如"购物车","我的"
}
var tabbarId = tabbarData.find(checkData).id;
this.setData({
tabbarId: tabbarId
})
utils.tabbar("tabBar", this.data.tabbarId, this, tabbarData);
},
// Tabbar跳转
goTab:function(e){
utils.goTab(e, this, this.data.tabbarId)
}
})
这样效果就实现了,后台改变传递过来的参数,tabbar
的数量可以动态改变。
如果大家有什么更好的实现方法,欢迎讨论交流。