微信小程序自定义 tabbar
一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件。
查阅了海量的博客和文档之后,亲自踩坑。总结了三种在不使用微信小程序原生 tabbar的情况下自制 tabbar 的方法。并说说这几种方法各自的特色。
类 navigator 跳转方式
类 navigator 跳转方式是我自己起的名称,因为它的实现思路就是这个样子的。期初参考 微信小程序自定义tabBar组件开发 这篇博文的思路。进行了这种方式的尝试,并为后续提供了解决思路。在这次实践的过程中使用了和该博文类似的目录结构。

template 文件主要包含了 tabbar 的内容、逻辑、模板、样式。
tabbar_template.js
//初始化数据
function tabbarinit() {
return [
{
"current": 0,
"pagePath": "/pages/travel_shop/travel/travel_index/travel_index",
"iconPath": "/pages/img/tab_icon_home@2x.png",
"selectedIconPath": "/pages/img/tab_icon_home_sel@2x.png",
"text": "首页"
},
{
"current": 0,
"pagePath": "/pages/travel_shop/travel/travel_car/travel_car",
"iconPath": "/pages/img/tab_icon_shop@2x.png",
"selectedIconPath": "/pages/img/tab_icon_shop_sel@2x.png",
"text": "购物车"
},
{
"current": 0,
"pagePath": "/pages/travel_shop/travel/travel_my/travel_my",
"iconPath": "/pages/img/tab_icon_my@2x.png",
"selectedIconPath": "/pages/img/tab_icon_my_sel@2x.png",
"text": "我的"
}
] }
//tabbar 主入口
function tabbarmain(bindName = "tabdata", id, target) {
var that = target;
var bindData = {};
var otabbar = tabbarinit();
otabbar[id]['iconPath'] = otabbar[id]['selectedIconPath'] //换当前的icon
otabbar[id]['current'] = 1;
bindData[bindName] = otabbar
that.setData({ bindData });
} module.exports = {
tabbar: tabbarmain
}
tabbar_template.wxml
<template name="tabBar">
<view class="tabBar">
<block wx:for="{{tabBar}}" wx:for-item="item" wx:key="tabBar">
<view class="tabBar-item">
<navigator open-type="reLaunch" url="{{item.pagePath}}">
<view><image class="tabBar-icon" src='{{item.iconPath}}'></image></view>
<view class="{{item.current== 1 ? 'tabBartext' :''}}">{{item.text}}</view>
</navigator>
</view>
</block>
</view>
</template>
tabbar_template.wxss
.tabBar-icon{
width:54rpx;
height: 54rpx;
}
.tabBar{
width:100%;
position: fixed;
bottom:;
padding:10rpx;
margin-left:-4rpx;
background:#F7F7FA;
font-size:24rpx;
color:#8A8A8A;
box-shadow: 3rpx 3rpx 3rpx 3rpx #aaa;
z-index:;
}
.tabBar-item{
float:left;
width: 33.333%;
text-align: center;
overflow: hidden;
}
/*当前字体颜色*/
.tabBartext{
color: black;
}
.navigator-hover{
background-color: rgba(0, 0, 0, 0);
}
而后在全局引入样式
@import "/pages/travel_shop/travel/tabbar_template/tabbar_template.wxss";
并在每一个页面的子文件(wxml、JS)中引入相应的内容
wxml 引入
<import src="/pages/travel_shop/travel/tabbar_template/tabbar_template.wxml"/>
<template is="tabBar" data="{{tabBar:bindData.tabBar}}"/>
JS 引入
var template = require("../tabbar_template/tabbar_template.js");
并在对应的 onLoad 生命周期中,注明它是哪一个 tabbar
onLoad: function (options) {
template.tabbar("tabBar", 1, this) //0表示第一个tabbar,这里1表示第二个 tabbar 的 icon
},
效果预览

我们最终得到了效果,但这种效果带了明显的抖动闪烁。原因则是因为这种实现方式的本质是通过 navigator 和 JS 事件触发实现页面之间的跳转。因此我开始找寻另一种实现的方式。在 微信小程序自定义tabBar组件开发 这篇博客的留言板,我发现该文的作者也发现了这种方式的不足,并提到可以通过可以把页面都写成组件 component 的方式实现更好的效果。
template 模板 / component 组件
在继续查阅了一些关于小程序自定义 tabbar 的博客之后,找到了 微信小程序 - 自定义tabbar 这篇博文。按照这篇博文描述的结构,我也进行了尝试。发现这种方式不会出现之前跳转产生的那种闪烁现象出现。

之后再查阅 微信小程序 template 模板与 component 组件的区别和使用 这篇博文的时候了解到,如果当我们主要是为了展示页面的时候,可以使用 template 方式。如果涉及到 tabbar 对应各个页面的业务逻辑交互比较多,那就最好使用 component 组件。
因为这三个页面涉及到了很多独立的交互,所以我决定使用 component 组件的形式,将自定义的 tabbar 写成一个页面,然后将其他三个 tabbar 按钮对应的页面写成三个 component 组件。这种方法和 Vue 中的组件化很相似,可以把单个组件文件夹当成 Vue 中的一个 .vue 文件。
component 与普通 page 类似,但是 JS 文件和 JSON 文件与页面不同。
小程序组件 JS 模板
Component({
/* 开启全局样式使用 */
options: {
addGlobalClass: true,
},
/* 组件的属性列表 */
properties: {
name: {
type: String,
value: ''
}
},
/* 组件的初始数据 */
data: {
},
/* 生命周期函数 */
lifetimes: {
attached: function () { },
moved: function () { },
detached: function () { },
},
/* 组件的方法列表 */
methods: {
},
})
component 组件 JSON 文件
{
"component": true,
"usingComponents": {}
}
tabbar 引用和配置
引用组件 JSON
按照如图的结构,三个 component 作为子组件,tabber 作为一个父级,因此它的 JSON 需要引入这三个 component 组件。

// travel.json
{
"usingComponents": {
"travel_car": "travel_car/travel_car",
"travel_index": "travel_index/travel_index",
"travel_my": "travel_my/travel_my"
}
}
tabbar JS
而该页面的 JS 仅仅只用来控制 tabbar 的 icon 选择,和传递一个 index 告诉页面该隐藏和显示哪一个 component 组件。
// travel.js
let app = getApp() Page({
data: {
currentTab: 0,
items: [
{
"iconPath": "/pages/img/tab_icon_home@2x.png",
"selectedIconPath": "/pages/img/tab_icon_home_sel@2x.png",
"text": "首页"
},
{
"iconPath": "/pages/img/tab_icon_shop@2x.png",
"selectedIconPath": "/pages/img/tab_icon_shop_sel@2x.png",
"text": "购物车"
},
{
"iconPath": "/pages/img/tab_icon_my@2x.png",
"selectedIconPath": "/pages/img/tab_icon_my_sel@2x.png",
"text": "我的"
}
]
},
//事件处理函数
bindChange: function (e) {
let that = this;
that.setData({
currentTab: e.detail.current
});
},
swichNav: function (e) {
let that = this;
if (this.data.currentTab === e.target.dataset.current) {
return false;
} else {
that.setData({
currentTab: e.target.dataset.current
})
}
},
onLoad: function () {
let that = this
app.getUserInfo(function (userInfo) {
that.setData({
userInfo: userInfo
})
})
}
})
tabbar WXML
直接使用之前 JSON 中引用过的标签名,类似于 Vue 中使用模板标签。这里由于组件模板标签不支持直接使用 hidden 属性,所以在外包裹了一层 view 标签用来添加 hidden属性。
<view hidden="{{currentTab == 0? false: true}}">
<travel_index/>
</view>
<view hidden="{{currentTab == 1? false: true}}">
<travel_car/>
</view>
<view hidden="{{currentTab == 2? false: true}}">
<travel_my/>
</view>
<view class="nav-tabs">
<view class="tab-list {{currentTab == idx ? 'active' : 'default' }}" wx:for="{{items}}" wx:key="prototype" wx:for-index="idx" wx:for-item="item" data-current="{{idx}}" bindtap="swichNav">
<text class="tab-text" wx:for-index="idx" data-current="{{idx}}" src="{{currentTab == idx ? item.selectedIconPath : item.iconPath }}">{{item.text}}</text>
<image class="iconPath" wx:for-index="idx" data-current="{{idx}}" src="{{currentTab == idx ? item.selectedIconPath : item.iconPath }}"></image>
</view>
</view>
tabbar WXSS
Some selectors are not allowed in component wxss, including tag name selectors, ID selectors, and attribute selectors.(./pages/xxx/xxx.wxss:288:3)This wxss file is ignored.
造成这种报错的原因是
component组件的样式中不能包含一些特定的选择器。
page {
display: flex;
flex-direction: column;
height: 100%;
}
.nav-tabs {
width: 100%;
display: flex;
position: fixed;
bottom:;
}
.tab-list {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column-reverse;
background: #fcfcfc;
}
.tab-text {
font-size: 24rpx;
line-height: 35rpx;
color: #5f5f5f;
}
.iconPath {
width:54rpx;
height: 54rpx;
}
.tab-content {
flex:;
}
.default {
line-height: 75rpx;
text-align: center;
flex:;
color: #eee;
font-weight: bold;
font-size: 28rpx;
}
.active {
line-height: 75rpx;
text-align: center;
color: black;
flex:;
font-weight: bold;
font-size: 28rpx;
}
.show {
display: block;
flex:;
}
.hidden {
display: none;
flex:;
}
预览效果
最终就完成了一个非原生小程序 tabbar 的自定义 tabbar 。

Github
在这篇文章发布之后,有一些朋友询问。 我重新整理了一个比较清晰整洁的 Demo 发布在了 GitHub。
如果这个 Demo 能够帮助到您。请不要吝惜您的 Star
微信小程序自定义 tabbar的更多相关文章
- 微信小程序自定义tabbar的实现
微信小程序自定义tabbar的实现 目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果:当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置. 方案:自己动 ...
- 微信小程序自定义tabbar的问题
个人感觉小程序的tab样式自定义的能力有所欠缺,不够美观,于是今天自己diy了一个tab 测试的时候发现,无论是使用navigator跳转(会出现点击的效果)还是用bindtap(触摸),因为没有定义 ...
- 微信小程序 自定义tabbar实例
在小程序的开发文档中,对tabbar是这样说明的: 如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以通过 tabBar 配置项指定 tab 栏的表现,以及 t ...
- 微信小程序自定义TabBar
项目中需要根据用户角色控制TabBar中各Item的显示和隐藏,然而小程序自带TabBar没有提供隐藏某个item的功能,只好自己动手写了一个. 1.wxml文件 <view class=&qu ...
- 微信小程序 - 自定义tabbar
更新: 2019-1-18:自定义tabbar组件已发布 各种奇葩的需求,造就了我们 wxml <view class="nav-tabs"> <view cla ...
- 微信小程序 - 自定义tabbar(组件)
配置项(关于使用组件) index.wxml <!-- tabBar:tabBar配置 activeIndex: 激活页面下标 slots: 多插槽配置(需与页面一致) --> <t ...
- 微信小程序——自定义导航栏
微信头部导航栏可能通过json配置: 但是有时候我们项目需求可能需要自定义头部导航栏,如下图所示: 现在具体说一下实现步骤及方法: 步骤: 1.在 app.json 里面把 "navigat ...
- 微信小程序自定义弹窗wcPop插件|仿微信弹窗样式
微信小程序自定义组件弹窗wcPop|小程序消息提示框|toast自定义模板弹窗 平时在开发小程序的时候,弹窗应用场景还是蛮广泛的,但是微信官方提供的弹窗比较有局限性,不能自定义修改.这个时候首先想到的 ...
- 微信小程序-自定义底部导航
代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
随机推荐
- 20165318 2017-2018-2 《Java程序设计》第三周学习总结
20165318 2017-2018-2 <Java程序设计>第三周学习总结 学习总结 我感觉从这一章开始,新的知识点扑面而来,很多定义都是之前没有接触过的,看书的时候难免有些晦涩.但由于 ...
- Odoo前端页面模版渲染引擎——Jinja2用法教程
转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9307200.html 一:渲染模版 要渲染一个qweb模板文件,通过render_template方法即可. ...
- kubernetes API Server 权限管理实践
API Server权限控制方式介绍 API Server权限控制分为三种:Authentication(身份认证).Authorization(授权).AdmissionControl(准入控制). ...
- Ubuntu下查看自己的GPU型号
1.在命令行中输入:lspci 即可看到当前显卡型号. 2.Ubuntu 14.04 安装 Nvidia 私有驱动 sudo apt-get install nvidia-331 3.进行双显卡切换n ...
- 串口调试助手vc源程序及其详细编写过程
串口调试助手vc源程序及其详细编写过程 目次: 1.建立项目 2.在项目中插入MSComm控件 3.利用ClassWizard定义CMSComm类控制变量 4.在对话框中添加控件 5.添加串口事件 ...
- iOS开发过程中易犯的小错误
addGestureRecognizer(_:) 一个手势对象只绑定一个view // 只有最后一个imgv有点击事件 let tap = UITapGestureRecognizer(target: ...
- bapi获取物料的可用数量及MRP信息(MD04)
需求:在报表里面添加可用数量字段,数据来源于MD04. 使用到的bapi:MD_STOCK_REQUIREMENTS_LIST_API 这个bapi可以查看到MRP信息以及可用数量. bapi需要的参 ...
- mysql/mariadb学习记录——创建删除数据库、表的基本命令
查看已有的数据库: mysql> show databases; +--------------------+ | Database | +--------------------+ | inf ...
- linux 学习第二十天(mariadb配置)
一.mariadb yum install mariadb mariadb-server systemctl restart mariadb systemctl enable mariadb mysq ...
- Scala的映射和元组操作
映射和元组操作 构造Map // 构造一个不可变的MAP映射,类似与key -> value这样的组合叫做对偶 val score = Map("Jack" -> 12 ...