微信小程序自定义TabBar
项目中需要根据用户角色控制TabBar中各Item的显示和隐藏,然而小程序自带TabBar没有提供隐藏某个item的功能,只好自己动手写了一个。
1、wxml文件
<view class="weui-flex tab-bar" style="color: {{color}}; background: {{backgroundColor}}; {{position=='top'? 'top: 0' : 'bottom: 0'}}; {{borderStyle? (position=='top'? 'border-bottom: solid 1px '+borderStyle + ';' : 'border-top: solid 1px '+borderStyle + ';') : ''}}">
<block wx:for="{{list}}" wx:key="pagePath">
<navigator url="{{item.pagePath}}" open-type="switchTab" class="weui-flex__item menu-item {{item.class}}" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : selectedColor) : ''}}" wx:if="{{item.hidden!==true}}">
<image src="{{item.selectedIconPath}}" wx:if="{{item.active}}" class="img"></image>
<image src="{{item.iconPath}}" wx:if="{{!item.active}}" class="img"></image>
<text class='tabbar_text'>{{item.text}}</text>
</navigator>
</block>
<view class="clear"></view>
</view>
重点:通过每个item的hidden属性控制是否显示
2、js文件
Component({
options: {
multipleSlots: true // 在组件定义时的选项中启用多slot支持
},
/**
* 组件的属性列表
*/
properties: {
color:{
type:String,
value:''
},
selectedColor:{
type:String,
value:'#1aad19'
},
backgroundColor:{
type:String,
value:''
},
position:{
type:String,
value:'bottom'
},
borderStyle:{
type: String,
value:'#ccc'
},
list:{
type:Array,
value:[]
}
},
/**
* 组件的初始数据
*/
data: {
},
/**
* 组件的方法列表
*/
methods: {
}
})
重点为list属性,定义为一个Array
3、wxss文件
@import "/style/weui.wxss";
.menu-item{
height:100rpx;
text-align: center;
padding-top: 5rpx;
}
.img{
width: 60rpx;
height: 60rpx;
display: block;
margin:auto;
}
.clear{
clear: both;
}
.tab-bar{
position: fixed;
width:100%
}
.tabbar_text{
font-size: 28rpx;
position:relative;
top:-12rpx;
}
4、使用组件:
在app.js中定义各Tab页签,并根据角色控制是否显示:
getTabList:function(){
var tabItemList = [
{
"pagePath": "/pages/asset/list",
"text": "资产台帐",
"iconPath": "/img/list_gray.png",
"selectedIconPath": "/img/list_blue.png",
"active": false
},
{
"pagePath": "/pages/check/index",
"text": "资产盘点",
"iconPath": "/img/list_gray.png",
"selectedIconPath": "/img/list_blue.png",
"active": false
},
{
"pagePath": "/pages/mine/index",
"text": "个人中心",
"iconPath": "/img/list_gray.png",
"selectedIconPath": "/img/list_blue.png",
"active": false
}
];
return tabItemList;
},
initTabBar:function(activeIdx){
var tabItemList=this.getTabList();
//资产台账,资产管理员可见
tabItemList[0].hidden=!this.isADMIN();
if(activeIdx>=0 && activeIdx<tabItemList.length){
tabItemList[activeIdx].active=true;
}
return tabItemList;
}
在页面的wxml中插入组件:
<view class="page">
<view class="page__bd">
<block wx:if='{{isSA}}'>
<view class="weui-cells__title"></view>
<view class="weui-cells weui-cells_after-title">
<navigator url="../config/index" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
<view class="weui-cell__bd">系统配置</view>
<view class="weui-cell__ft weui-cell__ft_in-access"></view>
</navigator>
</view>
</block>
<view class="weui-cells__title"></view>
<view class="weui-cells weui-cells_after-title">
<navigator url="changePassword" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
<view class="weui-cell__bd">修改密码</view>
<view class="weui-cell__ft weui-cell__ft_in-access"></view>
</navigator>
<view class="weui-cell weui-cell_access" hover-class="weui-cell_active" catchtap="logout">
<view class="weui-cell__bd">退出登录</view>
<view class="weui-cell__ft weui-cell__ft_in-access"></view>
</view>
</view>
</view>
</view>
<mfmtTabBar list="{{tabItemList}}"></mfmtTabBar>
页面js:
onShow: function () {
//初始化主Tab标签
var tabItemList = app.initTabBar(2);
this.setData({ tabItemList });
}
5、一个小问题
最初,定义组件的navigator时,使用openType="redirect",运行起来后,切换tab时,Tabbar有瞬间飞出去的感觉,用户体验很不好。
改为openType="switchTab",并在app.json中定义Tabbar(否则switchTab不生效):
"tabBar": {
"list": [
{
"pagePath": "pages/asset/list",
"text": "资产台帐"
},
{
"pagePath": "pages/check/index",
"text": "资产盘点"
},
{
"pagePath": "pages/mine/index",
"text": "个人中心"
}
]
}
在app.js的onlaunch方法里调用wx.hideTabBar接口,隐藏原有TabBar。
问题解决,切换时不再有“飞出去”的感觉
微信小程序自定义TabBar的更多相关文章
- 微信小程序自定义 tabbar
一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tab ...
- 微信小程序自定义tabbar的实现
微信小程序自定义tabbar的实现 目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果:当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置. 方案:自己动 ...
- 微信小程序自定义tabbar的问题
个人感觉小程序的tab样式自定义的能力有所欠缺,不够美观,于是今天自己diy了一个tab 测试的时候发现,无论是使用navigator跳转(会出现点击的效果)还是用bindtap(触摸),因为没有定义 ...
- 微信小程序 自定义tabbar实例
在小程序的开发文档中,对tabbar是这样说明的: 如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以通过 tabBar 配置项指定 tab 栏的表现,以及 t ...
- 微信小程序 - 自定义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 ...
随机推荐
- tomcat发布项目如何通过域名直接访问
首先在服务器中找到tomcat安装后的文件夹,进入到conf目录下,找到server.xml文件 打开并修改,修改如下: 第一步:修改port,该值默认为8080,将其修改为80 第二步:修改defa ...
- F7+vue 物理返回键监听使用
以前使用的是纯F7,这个项目加了Vue进去,但碰到了一个问题,就是这样监听不到安卓物理键的返回,它是点击返回,直接推出程序,这个坑有点深,查了不少资料也问了不少人,最后在网上看到了别人的写的,自己也改 ...
- Form表单发送到服务器时的编码方式
---恢复内容开始--- 表单中的表单中enctype是设置表单的MIME编码. 所谓MIME编码,是指当服务器传送数据给客户端时,必须指定这个文件是什么类型,才能方便客户端调用相应的应用软件来打开该 ...
- Treap标准模板
这是Treap的模板程序,支持Left/Right Rotate,Find the maxnum/minnum,Find the predecessor/successor of a node,Add ...
- git--编写好代码文件后更新到git仓库流程
先说一下git仓库分类: Git分为三大部分存储区域1:工作区域(就是你打开编辑器的本地代码仓库)2:提交缓存区域(使用git add 命令暂时放置的区域)3:git远程仓库(使用git push命令 ...
- 使用MobaXterm远程连接Ubuntu,启动Octave,界面不能正常显示
使用MobaXterm远程连接Ubuntu,启动Octave,界面不能正常显示,键盘输入无法显示,解决方法如下. 在启动octave之前,输入这两个命令: export GTK_MODULES=gai ...
- axios 发送post请求的时候会发送两次
第一次发送的时候会先发送OPTIONS, 第二次才发送POST, 解决方法: 引用qs模块 安装qs依赖 npm install qs --save 引入qs依赖 import qs from 'qs ...
- Android studio报Error:(26, 13)-v7:27.错误的解决方法
1.报错图片 2.上图我画了红圈很明显就提示报错方向就是项目文件:build.gradle 3.解决方法如下 添加此方法到项目构建.gradle文件中: repositories { maven ...
- day44-Celery异步分布式
celery异步分布式Celery是一个python开发的异步分布式任务调度模块.Celery本身并不提供消息服务,使用第三方服务,也就是borker来传递任务,目前支持rebbimq,redis, ...
- PHP调用Python接口过程中所遇到的问题
在php调用python脚本时,首先把python相关组件安装完善.用Python命令检测运行脚本而不报错. 在调用接口的过程 1.没有打开的python脚本的权限 解决办法:把python脚本所在的 ...