当前位置: 首页 > news >正文

小程序自定义tabbar

前言

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

一、在app.json配置

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

"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中配置

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及选中的样式

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

<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

接收父组件传过来的值

properties: {tabbar: {type: Object}
},

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

在需要用到tabbar的页面引用

1.json中引用

 "usingComponents": {"tabBar": "/components/customTabBar/customTabBar",}

2.js

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

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

3.html中引用

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

<tabBar tabbar="{{tabBar}}"></tabBar>
http://www.lryc.cn/news/165332.html

相关文章:

  • 分布式系统第五讲:分布式事务及实现方案
  • 算法通关村17关 | 透析跳跃游戏
  • ARM接口编程—RTC(exynos 4412平台)
  • 数据分享|WEKA信贷违约预测报告:用决策树、随机森林、支持向量机SVM、朴素贝叶斯、逻辑回归...
  • 逆市而行:如何在市场恐慌时保持冷静并抓住机会?
  • SpringBoot项目在Linux上启动、停止脚本
  • 基于32位单片机的感应灯解决方案
  • 机器学习——支持向量机(SVM)
  • HTTP协议初识·下篇
  • c++ 类的实例化顺序
  • Vue自动生成二维码并可下载二维码
  • 应该下那个 ActiveMQ
  • 【C语言】指针详解(3)
  • 告别HR管理繁琐,免费低代码平台来帮忙
  • Java开发面试--Redis专区
  • Ansible-roles学习
  • python3如何安装各类库的小总结
  • ffmpeg 特效 转场 放大缩小
  • 【GNN 03】PyG
  • 每日刷题-5
  • RNN简介(深入浅出)
  • Leetcode137. 某一个数字出现一次,其余数字出现3次
  • 原子化CSS(Atomic CSS)
  • pandas 筛选数据的 8 个骚操作
  • 【随想】每日两题Day.3(实则一题)
  • 阿里后端开发:抽象建模经典案例【文末送书】
  • HarmonyOS Codelab 优秀样例——溪村小镇(ArkTS)
  • Mybatis---第二篇
  • 6.2.3 【MySQL】InnoDB的B+树索引的注意事项
  • 前端面试话术集锦第 12 篇:高频考点(Vue常考基础知识点)