微信小程序自定义底部导航栏,切换不同页面显示不同tabbar

    在一个微信小程序中想要用到两种不同的tabbar样式,需要在app.js中自定义,在页面加载时进行调用。

比如一个小程序需要两个版本(用户版、商家版),并且能通过一个按钮在两个版本间进行切换,可能会用到这种方式。

    此处以两个页面(index,logs)显示两种tabbar样式为例,通过切换按钮进行切换。

    首先有一个模板文件:tabbar.wxml


   
   
  1. <template name="tabBar">
  2. <view class="tab-bar" style="color: {{tabBar.color}}; background: {{tarBar.backgroundColor}}; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}}; {{tabBar.borderStyle? (tabBar.position=='top'? 'border-bottom: solid 1px '+tabBar.borderStyle + ';' : 'border-top: solid 1px '+tabBar.borderStyle + ';') : ''}}">
  3. <block wx:for="{{tabBar.list}}" wx:key="pagePath">
  4. <navigator url="{{item.pagePath}}" open-type="redirect" class="{{item.clas}}" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : ''}}">
  5. <image src="{{item.selectedIconPath}}" wx:if="{{item.active}}" class="img"> </image>
  6. <image src="{{item.iconPath}}" wx:if="{{!item.active}}" class="img"> </image>
  7. <text>{{item.text}} </text>
  8. </navigator>
  9. </block>
  10. <view class="clear"> </view>
  11. </view>
  12. </template>

在app.json中无需定义“tabBar”

在app.js中自定义如下


   
   
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. },
  5. //第一种底部
  6. editTabBar: function () {
  7. //使用getCurrentPages可以获取当前加载中所有的页面对象的一个数组,数组最后一个就是当前页面。
  8. var curPageArr = getCurrentPages(); //获取加载的页面
  9. var curPage = curPageArr[curPageArr.length - 1]; //获取当前页面的对象
  10. var pagePath = curPage.route; //当前页面url
  11. if (pagePath.indexOf('/') != 0) {
  12. pagePath = '/' + pagePath;
  13. }
  14. var tabBar = this.globalData.tabBar;
  15. for (var i = 0; i < tabBar.list.length; i++) {
  16. tabBar.list[ i] .active = false;
  17. if ( tabBar.list[ i] .pagePath == pagePath) {
  18. tabBar.list[ i] .active = true; //根据页面地址设置当前页面状态
  19. }
  20. }
  21. curPage.setData({
  22. tabBar: tabBar
  23. });
  24. },
  25. //第二种底部,原理同上
  26. editTabBar1: function () {
  27. var curPageArr = getCurrentPages();
  28. var curPage = curPageArr[curPageArr.length - 1];
  29. var pagePath = curPage.route;
  30. if (pagePath.indexOf('/') != 0) {
  31. pagePath = '/' + pagePath;
  32. }
  33. var tabBar = this.globalData.tabBar1;
  34. for (var i = 0; i < tabBar.list.length; i++) {
  35. tabBar.list[i].active = false;
  36. if (tabBar.list[i].pagePath == pagePath) {
  37. tabBar.list[i].active = true;
  38. }
  39. }
  40. curPage.setData({
  41. tabBar: tabBar
  42. });
  43. },
  44. globalData: {
  45. //第一种底部导航栏显示
  46. tabBar: {
  47. "color": "#9E9E9E",
  48. "selectedColor": "#f00",
  49. "backgroundColor": "#fff",
  50. "borderStyle": "#ccc",
  51. "list": [
  52. {
  53. "pagePath": "/pages/index/index",
  54. "text": "职位",
  55. "iconPath": "/images/my.png",
  56. "selectedIconPath": "/images/my.png",
  57. "clas": "menu-item",
  58. "selectedColor": "#4EDF80",
  59. active: true
  60. },
  61. {
  62. "pagePath": "/pages/logs/logs",
  63. "text": "简历",
  64. "iconPath": "/images/my.png",
  65. "selectedIconPath": "/images/my.png",
  66. "selectedColor": "#4EDF80",
  67. "clas": "menu-item",
  68. active: false
  69. },
  70. {
  71. "pagePath": "/pages/test/test",
  72. "text": "我的",
  73. "iconPath": "/images/my.png",
  74. "selectedIconPath": "/images/my.png",
  75. "selectedColor": "#4EDF80",
  76. "clas": "menu-item",
  77. active: false
  78. }
  79. ],
  80. "position": "bottom"
  81. },
  82. //第二种底部导航栏显示
  83. tabBar1: {
  84. "color": "#9E9E9E",
  85. "selectedColor": "#f00",
  86. "backgroundColor": "#fff",
  87. "borderStyle": "#ccc",
  88. "list": [
  89. {
  90. "pagePath": "/pages/index/index",
  91. "text": "首页",
  92. "iconPath": "/images/my.png",
  93. "selectedIconPath": "/images/my.png",
  94. "clas": "menu-item1",
  95. "selectedColor": "#4EDF80",
  96. active: false
  97. },
  98. {
  99. "pagePath": "/pages/logs/logs",
  100. "text": "消息",
  101. "iconPath": "/images/my.png",
  102. "selectedIconPath": "/images/my.png",
  103. "selectedColor": "#4EDF80",
  104. "clas": "menu-item1",
  105. active: true
  106. },
  107. {
  108. "pagePath": "/pages/cont/index",
  109. "text": "简历",
  110. "iconPath": "/images/my.png",
  111. "selectedIconPath": "/images/my.png",
  112. "selectedColor": "#4EDF80",
  113. "clas": "menu-item1",
  114. active: false
  115. },
  116. {
  117. "pagePath": "/pages/detail/index",
  118. "text": "我的",
  119. "iconPath": "/images/my.png",
  120. "selectedIconPath": "/images/my.png",
  121. "selectedColor": "#4EDF80",
  122. "clas": "menu-item1",
  123. active: false
  124. }
  125. ],
  126. "position": "bottom"
  127. }
  128. }
  129. })

在app.wxss中定义显示样式


   
   
  1. .menu-item{
  2. width: 32%;
  3. float: left;
  4. text-align: center;
  5. padding-top: 8px;
  6. }
  7. .menu-item1{
  8. width: 24%;
  9. float: left;
  10. text-align: center;
  11. padding-top: 8px;
  12. }
  13. .img{
  14. width: 23px;
  15. height: 23px;
  16. display: block;
  17. margin:auto;
  18. }
  19. .clear{
  20. clear: both;
  21. }
  22. .tab-bar{
  23. position: fixed;
  24. width: 100%;
  25. padding: 0px 2%;
  26. }
  27. .button{
  28. margin: 130px;
  29. }

index.wxml,用到自定义tabbar的页面的首部都需要引入模板文件


   
   
  1. <import src="../../template/tabbar.wxml"/>
  2. <template is="tabBar" data="{{tabBar}}"/>
  3. 第一种底部导航栏样式的页面
  4. <button bindtap='tologs' size='mini' class="button">点击切换 </button>

index.js


   
   
  1. //index.js
  2. var app = getApp();
  3. Page({
  4. data: {
  5. },
  6. onShow:function(){
  7. app.editTabBar();     //显示自定义的底部导航
  8. },
  9. tologs:function(){     //按钮的绑定事件,点击跳转至logs
  10. wx.redirectTo({
  11. url: '../logs/logs',
  12. })
  13. },
  14. onLoad: function () {
  15. }
  16. })

logs.wxml


   
   
  1. <import src="../../template/tabbar.wxml"/>
  2. <template is="tabBar" data="{{tabBar}}"/>
  3. 第二种样式页面
  4. <button bindtap='toindex' size='mini' class="button">点击切换 </button>

logs.js


   
   
  1. //logs.js
  2. var app = getApp();
  3. Page({
  4. data: {
  5. },
  6. //点击按钮跳转页面
  7. toindex: function () {
  8. wx.redirectTo({
  9. url: '../index/index',
  10. })
  11. },
  12. onLoad: function () {
  13. //加载本页面的tabBar样式
  14. app.editTabBar1();
  15. }
  16. })

加载自定义tabbar的那句话(app.editTabBar)写在onload或onshow中都可以。

只写了两个主页面,其他页面可自行定义跳转。

最后放上效果图:

                 

点击切换按钮,可在两种底部导航样式的页面进行切换。本文主要为初步实现功能,样式不美观勿介意。

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值