一定的需求情况下,无法使用小程序原生的 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. NSKeyValueObserving.m

    https://github.com/farcaller/cocotron/blob/af740de86c9bee84c59ffc74d27e5df9e22e1391/Foundation/NSKey ...

  2. ansible--03

    一. Ad-hoc命令简介 1. 格式:ansible <host> [opion] 2. option参数: -v:输出详细的执行过程, -vvv最详细的结果 -i:指定inventor ...

  3. TensorFlow函数(八)tf.control_dependencies()

    tf.control_dependencies(control_inputs) 此函数指定某些操作执行的依赖关系 返回一个控制依赖的上下文管理器,使用 with 关键字可以让在这个上下文环境中的操作都 ...

  4. ethereumjs/ethereumjs-account-2-test

    ethereumjs-account/test/index.js const Account = require('../index.js') const tape = require('tape') ...

  5. vux UI 项目国际化

    第一步必须装 vux vux-loader vuex 和vuex-i18n npm i vux-loader -D npm i vuex vux vuex-i18n -S 安装完成需要配置webpac ...

  6. kendo ui - DropDownList 下拉列表系列

    kendo-ui 官网:https://www.telerik.com/documentation 初始化 grid: 引入文件: <link rel="stylesheet" ...

  7. 51 Nod 1107 斜率小于0的连线数量 (转换为归并求逆序数或者直接树状数组,超级详细题解!!!)

    1107 斜率小于0的连线数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题   二维平面上N个点之间共有C(n,2)条连线.求这C(n,2)条线中斜率小于0的线 ...

  8. 简单部署iRedMail-0.9.8 - 邮件服务器架构和错误代码

    1.去官网下载最新稳定版软件 https://www.iredmail.com/index.html 2.https://docs.iredmail.org/install.iredmail.on.r ...

  9. Using 1.7 requires compiling with Android 4.4 (KitKat); currently using

    今天编译一个project,我设置为api 14,可是编译报错: Using 1.7 requires compiling with Android 4.4 (KitKat); currently u ...

  10. jsonp封装方法二

    前言: 看到玉伯的聊聊jsonp的p,引发了另一种loader方式来跨域的方法,他把它叫做JSONM协议,原理和seajs相似,都是动态加载script,加载完成后执行callback, 同时还不用考 ...