小程序自定义tabbar

前言

使用小程序默认的tabbar可以满足常规开发,但是满足不了个性化需求,如果想个性化开发就需要用到自定义tabbar,以下图为例子

一、在app.json配置

先按照以往默认的形式配置,如果中间的样式特殊则不需要配置

javascript 复制代码
"tabBar": {
  "custom":true,  // 开启自定义tabbar
  "color": "#333333",  // tabbar文字默认颜色
  "selectedColor": "#2E41FF", // tabbar文字选中颜色
  "list": [
    {
      "pagePath": "pages/index/index",
      "iconPath": "images/customTabBar/home.png",
      "selectedIconPath": "images/customTabBar/homeSelect.png",
      "text": "首页"
    },
    {
      "pagePath": "pages/user/user",
      "iconPath": "images/customTabBar/my.png",
      "selectedIconPath": "images/customTabBar/mySelect.png",
      "text": "我的"
    }
  ]
},

二、在app.js中配置

javascript 复制代码
tabBar: {
  "color": "#333333",
  "selectedColor": "#2E41FF",
  "list": [{
      "pagePath": "/pages/index/index",
      "iconPath": "/images/customTabBar/home.png",
      "selectedIconPath": "/images/customTabBar/homeSelect.png",
      "text": "首页",
      "selected":true  //该tabbar是否选中的标识
    },
    {
      "pagePath": '',
      "iconPath": '/images/customTabBar/camera.png',
      "selectedIconPath": '/images/customTabBar/camera.png',
      "isSpecial": true,  //是否为特殊的那个tab
      "selected":false
    },
    {
      "pagePath": "/pages/user/user",
      "iconPath": "/images/customTabBar/my.png",
      "selectedIconPath": "/images/customTabBar/mySelect.png",
      "text": "我的",
      "selected":false
    }
  ]
}

三、app.js注册tabbar点击方法

主要作用为控制跳转的路径当前tab及选中的样式

javascript 复制代码
editTabbar() {
  let tabbar = this.globalData.tabBar;
  let currentPages = getCurrentPages();
  let _this = currentPages[currentPages.length - 1];
  let pagePath = _this.route;
  (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
  for (let i in tabbar.list) {
    tabbar.list[i].selected = false;
    (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
  }
  _this.setData({
    tabbar: tabbar
  });
},

四、封装自定义tabbar组件

1.html

html 复制代码
<view class="ub-tab-bar">
  <block wx:for="{{tabbar.list}}" wx:key="index">
  <!-- 中间特殊tab -->
    <view wx:if="{{item.isSpecial}}" class="ub-tab-bar-item" bindtap="camera">
      <image mode="widthFix" src="{{item.iconPath}}" class="{{item.pagePath === '' ? 'ub-camera' : ''}}"></image>
      <view>{{item.text}}</view>
    </view>
  <!-- 两侧tab -->  
    <navigator wx:else class="ub-tab-bar-item" hover-class="none" url="{{item.pagePath}}" style="color:{{item.selected ? tabbar.selectedColor : tabbar.color}}" open-type="switchTab">
      <image mode="widthFix"  src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image>
      <view>{{item.text}}</view>
    </navigator>
  </block>
</view>

2.json

接收父组件传过来的值

javascript 复制代码
properties: {
  tabbar: {
    type: Object
  }
},

五、在用到的页面引用tabbar组件

在需要用到tabbar的页面引用

1.json中引用

javascript 复制代码
 "usingComponents": {
    "tabBar": "/components/customTabBar/customTabBar",
  }

2.js

data中获取在app.js定义的tabBar

javascript 复制代码
data: {
	tabBar:app.globalData.tabBar
}
//隐藏移动的tabbar并调用app.js定义的方法
onLoad(){
	wx.hideTabBar()
	app.editTabbar();
}

3.html中引用

获取到的方法想子组件中传递

html 复制代码
<tabBar tabbar="{{tabBar}}"></tabBar>
相关推荐
云起SAAS15 小时前
倒班日历助手抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·倒班日历助手
sheji341616 小时前
【开题答辩全过程】以 基于微信小程序的失物认领系统为例,包含答辩的问题和答案
微信小程序·小程序
qq_256847888617 小时前
08cms房产多城市版最新v8.7含小程序及装修网的安装及配置方法整理出来希望能帮到大家
小程序·08cms房产系统源码·08cms装饰网门户系统·08cms 房产门户系统源码
风月歌17 小时前
python项目之摄影竞赛小程序
python·mysql·小程序·毕业设计·源码
云云只是个程序马喽17 小时前
2026年短剧系统开发搭建全流程教程(小程序+APP自营/红果模式/广告联盟/海外多语言)
小程序
计算机毕设指导617 小时前
基于微信小程序的网络安全知识科普平台系统【源码文末联系】
java·spring boot·安全·web安全·微信小程序·小程序·tomcat
天***88962 天前
小程序婚纱店摄影楼预约婚庆礼展示,小程序开发定制,会员下单档期系统
小程序
说私域2 天前
开源AI智能名片链动2+1模式商城小程序下短视频电商变现与广告变现的对比研究
人工智能·小程序
天***88962 天前
美业医疗美容院小程序,预约会员管理养生馆诊所肌护肤理疗系统,附源码交付
小程序
2501_915909062 天前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview