uni-app底部导航栏tabBar监听变化以及变换样式

2023-11-13

一、简介

tabBar有三项,点击后两项变换tabBar的样式

二、案例演示

在这里插入图片描述

三、代码

1.首先,监听tabBar 点击切换,放在这三个页面,和onLoad同级。页面生命周期onTabItemTap

**
 * 监听 TabBar 切换点击
 */
onTabItemTap: function(item) {
	console.log(item)
}

2.更改图片路径和汉字

page.json:【默认】

"tabBar": {
	"color": "#E0E3ED",
	"selectedColor": "#ffffff",
	"borderStyle": "white",
	"backgroundColor": "#3A57A1",
	"iconWidth": "18px",
	"spacing": "5px",
	"list": [{
			"pagePath": "pages/index/index",
			"iconPath": "static/tabBarBlue/index1.png",
			"selectedIconPath": "static/tabBarBlue/index2.png",
			"text": "首页"
		},
		{
			"pagePath": "pages/mall/mall",
			"iconPath": "static/tabBarBlue/mall1.png",
			"selectedIconPath": "static/tabBarBlue/mall2.png",
			"text": "商城"
		},
		{
			"pagePath": "pages/mine/mine",
			"iconPath": "static/tabBarBlue/mine1.png",
			"selectedIconPath": "static/tabBarBlue/mine2.png",
			"text": "我的"
		}
	]
},

index.vue

/**
* 监听 TabBar 切换点击
 */
onTabItemTap: function(item) {
	console.log(item)
	// 点击了首页
	if (item.index == 0) {
		console.log("首页");
		uni.setTabBarItem({
			index: 0,
			text: '首页',
			iconPath: 'static/tabBarBlue/index1.png', //图片路径
			selectedIconPath: 'static/tabBarBlue/index2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 1,
			text: '商城',
			iconPath: 'static/tabBarBlue/mall1.png', //图片路径
			selectedIconPath: 'static/tabBarBlue/mall2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 2,
			text: '我的',
			iconPath: 'static/tabBarBlue/mine1.png', //图片路径
			selectedIconPath: 'static/tabBarBlue/mine2.png' //选中时的图片路径
		})
		uni.setTabBarStyle({
			color: '#E0E3ED', //tab 上的文字默认颜色
			selectedColor: '#ffffff', //tab 上的文字选中时的颜色
			backgroundColor: '#3A57A1', //tab 的背景色,HexColor
			borderStyle: 'white' //tabBar上边框的颜色, 仅支持 black/white
		})
	}
	// 点击了商城
	if (item.index == 1) {
		console.log("商城");
		uni.setTabBarItem({
			index: 1,
			text: '商城',
			iconPath: 'static/tabBarWhite/mall1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mall2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 0,
			text: '首页',
			iconPath: 'static/tabBarWhite/index1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/index2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 2,
			text: '我的',
			iconPath: 'static/tabBarWhite/mine1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mine2.png' //选中时的图片路径
		})
		uni.setTabBarStyle({
			color: '#262626', //tab 上的文字默认颜色
			selectedColor: '#35519B', //tab 上的文字选中时的颜色
			backgroundColor: '#ffffff', //tab 的背景色,HexColor
			borderStyle: 'black' //tabBar上边框的颜色, 仅支持 black/white
		})
	}
	// 点击了我的
	if (item.index == 2) {
		console.log("我的");
		uni.setTabBarItem({
			index: 2,
			text: '我的',
			iconPath: 'static/tabBarWhite/mine1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mine2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 0,
			text: '首页',
			iconPath: 'static/tabBarWhite/index1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/index2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 1,
			text: '商城',
			iconPath: 'static/tabBarWhite/mall1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mall2.png' //选中时的图片路径
		})
		uni.setTabBarStyle({
			color: '#262626', //tab 上的文字默认颜色
			selectedColor: '#35519B', //tab 上的文字选中时的颜色
			backgroundColor: '#ffffff', //tab 的背景色,HexColor
			borderStyle: 'black' //tabBar上边框的颜色, 仅支持 black/white
		})
	}
},

mall.vue

/**
 * 监听 TabBar 切换点击
 */
onTabItemTap: function(item) {
	console.log(item)
	// 点击了首页
	if (item.index == 0) {
		console.log("首页");
		uni.setTabBarItem({
			index: 0,
			text: '首页',
			iconPath: 'static/tabBarBlue/index1.png', //图片路径
			selectedIconPath: 'static/tabBarBlue/index2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 1,
			text: '商城',
			iconPath: 'static/tabBarBlue/mall1.png', //图片路径
			selectedIconPath: 'static/tabBarBlue/mall2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 2,
			text: '我的',
			iconPath: 'static/tabBarBlue/mine1.png', //图片路径
			selectedIconPath: 'static/tabBarBlue/mine2.png' //选中时的图片路径
		})
		uni.setTabBarStyle({
			color: '#E0E3ED', //tab 上的文字默认颜色
			selectedColor: '#ffffff', //tab 上的文字选中时的颜色
			backgroundColor: '#3A57A1', //tab 的背景色,HexColor
			borderStyle: 'white' //tabBar上边框的颜色, 仅支持 black/white
		})
	}
	// 点击了商城
	if (item.index == 1) {
		console.log("商城");
		uni.setTabBarItem({
			index: 1,
			text: '商城',
			iconPath: 'static/tabBarWhite/mall1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mall2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 0,
			text: '首页',
			iconPath: 'static/tabBarWhite/index1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/index2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 2,
			text: '我的',
			iconPath: 'static/tabBarWhite/mine1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mine2.png' //选中时的图片路径
		})
		uni.setTabBarStyle({
			color: '#262626', //tab 上的文字默认颜色
			selectedColor: '#35519B', //tab 上的文字选中时的颜色
			backgroundColor: '#ffffff', //tab 的背景色,HexColor
			borderStyle: 'black' //tabBar上边框的颜色, 仅支持 black/white
		})
	}
	// 点击了我的
	if (item.index == 2) {
		console.log("我的");
		uni.setTabBarItem({
			index: 2,
			text: '我的',
			iconPath: 'static/tabBarWhite/mine1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mine2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 0,
			text: '首页',
			iconPath: 'static/tabBarWhite/index1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/index2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 1,
			text: '商城',
			iconPath: 'static/tabBarWhite/mall1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mall2.png' //选中时的图片路径
		})
		uni.setTabBarStyle({
			color: '#262626', //tab 上的文字默认颜色
			selectedColor: '#35519B', //tab 上的文字选中时的颜色
			backgroundColor: '#ffffff', //tab 的背景色,HexColor
			borderStyle: 'black' //tabBar上边框的颜色, 仅支持 black/white
		})
	}
},

mine.vue

/**
* 监听 TabBar 切换点击
 */
onTabItemTap: function(item) {
	console.log(item)
	// 点击了首页
	if (item.index == 0) {
		console.log("首页");
		uni.setTabBarItem({
			index: 0,
			text: '首页',
			iconPath: 'static/tabBarBlue/index1.png', //图片路径
			selectedIconPath: 'static/tabBarBlue/index2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 1,
			text: '商城',
			iconPath: 'static/tabBarBlue/mall1.png', //图片路径
			selectedIconPath: 'static/tabBarBlue/mall2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 2,
			text: '我的',
			iconPath: 'static/tabBarBlue/mine1.png', //图片路径
			selectedIconPath: 'static/tabBarBlue/mine2.png' //选中时的图片路径
		})
		uni.setTabBarStyle({
			color: '#E0E3ED', //tab 上的文字默认颜色
			selectedColor: '#ffffff', //tab 上的文字选中时的颜色
			backgroundColor: '#3A57A1', //tab 的背景色,HexColor
			borderStyle: 'white' //tabBar上边框的颜色, 仅支持 black/white
		})
	}
	// 点击了商城
	if (item.index == 1) {
		console.log("商城");
		uni.setTabBarItem({
			index: 1,
			text: '商城',
			iconPath: 'static/tabBarWhite/mall1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mall2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 0,
			text: '首页',
			iconPath: 'static/tabBarWhite/index1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/index2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 2,
			text: '我的',
			iconPath: 'static/tabBarWhite/mine1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mine2.png' //选中时的图片路径
		})
		uni.setTabBarStyle({
			color: '#262626', //tab 上的文字默认颜色
			selectedColor: '#35519B', //tab 上的文字选中时的颜色
			backgroundColor: '#ffffff', //tab 的背景色,HexColor
			borderStyle: 'black' //tabBar上边框的颜色, 仅支持 black/white
		})
	}
	// 点击了我的
	if (item.index == 2) {
		console.log("我的");
		uni.setTabBarItem({
			index: 2,
			text: '我的',
			iconPath: 'static/tabBarWhite/mine1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mine2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 0,
			text: '首页',
			iconPath: 'static/tabBarWhite/index1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/index2.png' //选中时的图片路径
		})
		uni.setTabBarItem({
			index: 1,
			text: '商城',
			iconPath: 'static/tabBarWhite/mall1.png', //图片路径
			selectedIconPath: 'static/tabBarWhite/mall2.png' //选中时的图片路径
		})
		uni.setTabBarStyle({
			color: '#262626', //tab 上的文字默认颜色
			selectedColor: '#35519B', //tab 上的文字选中时的颜色
			backgroundColor: '#ffffff', //tab 的背景色,HexColor
			borderStyle: 'black' //tabBar上边框的颜色, 仅支持 black/white
		})
	}
},

四、总结

网址:https://uniapp.dcloud.net.cn/api/ui/tabbar.html#settabbaritem
onTabItemTap页面生命周期,点击 tab 时触发

uni.setTabBarItem(OBJECT)
动态设置 tabBar 某一项的内容

uni.setTabBarStyle(OBJECT)
动态设置 tabBar 的整体样式

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

uni-app底部导航栏tabBar监听变化以及变换样式 的相关文章

随机推荐

  • 最小二乘曲线拟合——C语言算法实现二

    最小二乘曲线拟合 在上一篇博客中我们介绍了最小二乘法的原理 以及代码实现的例子 http blog csdn net beijingmake209 article details 27565125 本次我们再给出一个程序实现的例子 编译环境
  • 解决atibtmon.exe错误

    笔记本装好win7之后 折腾了一番 用驱动精灵更新了所有驱动 又装了林林种种的软件 继而用QQ管家优化系统 后来 发现系统在启动时 经常报错 atibtmon exe 如下 在网上搜了一番 有的说重装显卡驱动 有的说要重装visual c
  • XPath crash course note

    XPath language What is XPath To be supplied What is XPath for To be supplied Why XPath To be supplied basic concepts exa
  • exception in thread main java.lang.NoClassDefFoundError wrong name解决方法

    初学java时 在执行java文件时 可能会遇到java代码能编译通过 但执行文件时出现 exception in thread main java lang NoClassDefFoundError wrong name的错误 当不含包层
  • linux 清空文件内容的方式

    目录 一 通过重定向来清空文件内容 gt 通过shell重定向null到指定文件即可 gt 重定向 或 true 到指定文件 二 使用cat cp dd使用工具和 dev null设备来清空文件内容 gt 可以使用cat命令查看 dev n
  • Excel实现数据的线性回归

    文章目录 一 下载安装Excel 二 使用Excel做线性回归 三 验证回归方程的精度 四 参考资料 一 下载安装Excel 一般购买的电脑都已经安装好了Microsoft Office 就不需要进行下载安装这一过程 百度网盘链接 http
  • 记录一下Object.entries()的用法

    Object entries 方法返回一个给定对象自身可枚举属性的键值对数组 其排列与使用 for in 循环遍历该对象时返回的顺序一致 区别在于 for in 循环还会枚举原型链中的属性 语法 Object entries obj 参数
  • 赛车游戏——【极品飞车】(内含源码inscode在线运行)

    前言 作者主页 雪碧有白泡泡 个人网站 雪碧的个人网站 推荐专栏 java一站式服务 前端炫酷代码分享 uniapp 从构建到提升 从0到英雄 vue成神之路 解决算法 一个专栏就够了 架构咱们从0说 数据流通的精妙之道 文章目录 前言 认
  • MFC ActiveX 接口数据类型,伤不起!

    要不是因为我一直在维护一个曲线绘制控件 www st curve cn 我真的很不想再碰MFC的ActiveX了 其中怎一个乱字了得 回想起来 似乎还是vc6最好 后来我相继升级到了vc2003 2008 2010 每次升级都让我很受伤 注
  • 【机器学习】多分类及多标签分类算法(含源代码)

    目录 多分类及多标签分类算法 一 单标签二分类问题 1 1 单标签二分类算法原理 二 单标签多分类问题 2 1 ovo 2 1 1 手写代码 2 1 2 调用API 2 2 ovr 2 2 1 手写代码 2 2 2 调用API 2 3 Ov
  • 2022年十月份电赛OpenMV巡线方案(2)---主控代码详细分析

    前言 1 继上一篇 2022年十月份电赛OpenMV巡线方案详细代码分析 1 2 这个代码适用于所有主控 只需要更改一下串口接收部分的API 别问我某某MCU能不能跑的这种废话 3 本文使用的协议与正点原子的串口通讯协议一致 看不懂的建议学
  • 当前系统环境打成tar包

    当前系统环境打成tar包 原文地址 https www cnblogs com alexkn p 3879540 html 命令 tar numeric owner exclude proc exclude sys zcvf centos7
  • 【PlayWright教程(一)】安装和使用(python)

    当今常用的三个自动化测试 或者爬虫 库 库 Selenium Puppeteer Playwright JavaScript 支持 官方支持 官方支持 官方支持 Python 异步支持 无 第三方 而且 bug 不少 官方支持 Python
  • 使用bibmap修改bib文件中参考文献的期刊或会议名的字母大小写格式为titlecase

    使用bibmap方便的修改bib文件中参考文献的期刊或会议名的字母大小写格式为titlecase 1 引言 英文语句的字母大小写形式有多种格式 常用的格式是 全大写 upper case 即句子的所有字母都大写 常见于学位论文 报告等的封面
  • system.data.sqlite的源代码下载

    帮助文档 http system data sqlite org index html doc trunk www index wiki 历史版本https system data sqlite org index html doc tru
  • Kendo UI开发教程(14): Kendo MVVM 数据绑定(三) Click

    Click绑定可以把由ViewModel定义的方法不绑定到目标DOM的click事件 当点击目标DOM元素时触发ViewModel的对应方法 例如 使用Click绑定 1
  • Redis有序集合和定时任务解决订单15分钟关闭

    直接上代码 下单减去库存 public String updatePersonStock PageData pd throws Exception Map
  • IPSec协议

    内容提要 Motivation IP协议的安全缺陷 虚拟专用网 IPSec概述 协议流程 SPD SAD 数据封装 IPSec AH IPSec ESP 安全参数协商 ISAKMP IKE 一 Motivation 1 1IP协议的安全缺陷
  • Google Chrome 扩展程序

    Adblock Plus 扩展网址 https chrome google com webstore detail adblock plus free ad bloc cfhdojbkjhnklbpkdaibdccddilifddb 官网
  • uni-app底部导航栏tabBar监听变化以及变换样式

    一 简介 tabBar有三项 点击后两项变换tabBar的样式 二 案例演示 三 代码 1 首先 监听tabBar 点击切换 放在这三个页面 和onLoad同级 页面生命周期onTabItemTap 监听 TabBar 切换点击 onTab