一定的需求情况下,无法使用小程序原生的 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的更多相关文章

  1. 微信小程序自定义tabbar的实现

    微信小程序自定义tabbar的实现 目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果:当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置. 方案:自己动 ...

  2. 微信小程序自定义tabbar的问题

    个人感觉小程序的tab样式自定义的能力有所欠缺,不够美观,于是今天自己diy了一个tab 测试的时候发现,无论是使用navigator跳转(会出现点击的效果)还是用bindtap(触摸),因为没有定义 ...

  3. 微信小程序 自定义tabbar实例

    在小程序的开发文档中,对tabbar是这样说明的: 如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以通过 tabBar 配置项指定 tab 栏的表现,以及 t ...

  4. 微信小程序自定义TabBar

    项目中需要根据用户角色控制TabBar中各Item的显示和隐藏,然而小程序自带TabBar没有提供隐藏某个item的功能,只好自己动手写了一个. 1.wxml文件 <view class=&qu ...

  5. 微信小程序 - 自定义tabbar

    更新: 2019-1-18:自定义tabbar组件已发布 各种奇葩的需求,造就了我们 wxml <view class="nav-tabs"> <view cla ...

  6. 微信小程序 - 自定义tabbar(组件)

    配置项(关于使用组件) index.wxml <!-- tabBar:tabBar配置 activeIndex: 激活页面下标 slots: 多插槽配置(需与页面一致) --> <t ...

  7. 微信小程序——自定义导航栏

    微信头部导航栏可能通过json配置: 但是有时候我们项目需求可能需要自定义头部导航栏,如下图所示: 现在具体说一下实现步骤及方法: 步骤: 1.在 app.json 里面把 "navigat ...

  8. 微信小程序自定义弹窗wcPop插件|仿微信弹窗样式

    微信小程序自定义组件弹窗wcPop|小程序消息提示框|toast自定义模板弹窗 平时在开发小程序的时候,弹窗应用场景还是蛮广泛的,但是微信官方提供的弹窗比较有局限性,不能自定义修改.这个时候首先想到的 ...

  9. 微信小程序-自定义底部导航

    代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

随机推荐

  1. python第十四课--排序及自定义函数之案例一:选择排序

    案例一:选择排序使用选择排序的思想实现列表数据的升序排序 lt=[45,12,56,-32,-3,44,75,-22,100] length=len(lt) # print('排序前:'+str(lt ...

  2. shell基础--test命令的使用

    test :用于文件类型检查和变量比较 一.用途: 1.判断表达式 2.判断字符串 3.判断整数 4.判断文件 测试例子: (1).test [root@~_~ day5]# cat test.sh ...

  3. 03.Java语言基础

    Java程序的组成 关键字,标识符,注释,变量,语句,表达式,数组,方法 关键字 Java语言内部使用了的一些用于特殊用途的词汇,那么在程序中用户不能使用.语言本身保留了一些词汇用于语言的语法等用途. ...

  4. Spring整合MyBatis(五)MapperScannerConfigurer

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 目录 一.processPropertyPlaceHolders属性的 ...

  5. Mybatis的使用与流程解析

    1. 什么是MyBatis MyBatis的前身是Apache的一个开源项目ibatis,后来迁移到Google code就改名为MyBatis. 官方解释: MyBatis是一款优秀的持久层框架,它 ...

  6. block本质探寻四之copy

    说明: <1>阅读本文,最好阅读之前的block文章加以理解: <2>本文内容:三种block类型的copy情况(MRC).是否深拷贝.错误copy: 一.MRC模式下,三种b ...

  7. Windows下修改iTunes备份路径

    0.准备工作: 关闭itunes 在任务管理器中杀掉iTunes开头的服务 1,找到iTunes默认备份路径:C:\Users\xxx\AppData\Roaming\Apple Computer\M ...

  8. ThreadLocal理解

    ThreadLocal 概述 ThreadLocal实例仅作为线程局部变量的==操作类==,以及==线程存储局部变量时的Key==.真正的线程局部变量是存储在各自线程的本地,通过Thread类中的Th ...

  9. GoLand(一)安装

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.安装包下载地址https://golang.org/ 二.Windows下安装:1.下载好.msi的安装包文件 ...

  10. 如何在mac上运行vue项目

    使用终端安装Vue运行环境 1.安装 Homebrew Homebrew 是osx下面最优秀的包管理工具,没有之一.先在终端查看是否已安装brew brew -v 如果返回 Homebrew 的版本号 ...