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

Uniapp之微信小程序自定义底部导航栏形态

发现微信小程序自带导航栏有些丑,只能改变一下常用图标字体颜色什么的,今天就搞个自定义的tab,效果如下。

一,首先就是配置pages.json文件,必须设置"custom": true,  // 关键配置:启用自定义 tabBar

"list" 里面还是和自带的一样,开始研究设置为空[] 但是运行不起来,提示必须得有之前哪些tab信息。

这是我的测试代码

"tabBar" : {"custom": true,  // 关键配置:启用自定义 tabBar"list" : [{"selectedIconPath" : "static/icons/home-active.png","iconPath" : "static/icons/home.png","pagePath" : "pages/index/index","text" : "首页"},{"selectedIconPath" : "static/icons/add-active.png","iconPath" : "static/icons/add.png","pagePath" : "pages/add/index","text" : "记账"},{"selectedIconPath" : "static/icons/analysis-active.png","iconPath" : "static/icons/analysis.png","pagePath" : "pages/analysis/index","text" : "分析"},{ "selectedIconPath" : "static/icons/mine-active.png","iconPath" : "static/icons/mine.png","pagePath" : "pages/mine/index","text" : "我的"}]}

二、设置 公用 tab 导航页面,新建 components--------custom-tabbar目录 及custom-tabbar.vue文件页面

custom-tabbar.vue 

<template><view class="custom-tabbar"><view class="tabbar-item" v-for="(item, index) in tabList" :key="index"@click="switchTab(item.path)"><!-- 图标 --><view class="icon-wrapper"><image :src="currentPath === item.path ? item.selectedIcon : item.icon" mode="widthFix"class="tabbar-icon"></image></view><!-- 文字 --><text class="tabbar-text":class="{ 'active': currentPath === item.path }">{{ item.text }}</text></view></view>
</template><script>
export default {data() {return {currentPath: '',  // 当前页面路径// 导航栏配置tabList: [{path: '/pages/index/index',text: '首页',icon: '/static/icons/home.png',selectedIcon: '/static/icons/home-active.png'},{path: '/pages/add/index',text: '记账',icon: '/static/icons/add.png',selectedIcon: '/static/icons/add-active.png'},{path: '/pages/analysis/index',text: '分析',icon: '/static/icons/analysis.png',selectedIcon: '/static/icons/analysis-active.png'},{path: '/pages/mine/index',text: '我的',icon: '/static/icons/mine.png',selectedIcon: '/static/icons/mine-active.png'}]}},
created() {// 监听全局路由变化this.routeListener = uni.onAppRoute((res) => {// 路由变化后,延迟获取页面栈(确保页面已切换完成)setTimeout(() => {const pages = getCurrentPages()this.currentPath = "/" + pages[pages.length - 1].routeconsole.log('路由变化,更新路径:', this.currentPath)}, 100)})},beforeDestroy() {// 销毁时移除监听,避免内存泄漏this.routeListener()},methods: {// 切换页面switchTab(path) {// 如果点击的是当前页面,不做操作if (this.currentPath === path) return// 使用 switchTab 跳转(适合 tab 页面)uni.switchTab({url: path})}}
}
</script><style scoped>
.custom-tabbar {display: flex;justify-content: space-around;align-items: center;position: fixed;bottom: 3%;left: 2%;right: 2%;height: 50px;border-radius: 25px;box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);z-index: 999;background: #fff;
}.tabbar-item {display: flex;flex-direction: column;align-items: center;justify-content: center;flex: 1;
}.icon-wrapper {height: 24px;margin-bottom: 2px;
}.tabbar-icon {width: 24px;height: 24px;
}.tabbar-text {font-size: 12px;color: #2c2c2c;
}.tabbar-text.active {color: #007aff;  /* 选中状态颜色 */
}
</style>

三、引用到所需要的页面即可,举例:index.vue

1,页面部分

<template><view class="container"><!-- 页面内容 --><custom-tabbar></custom-tabbar></view>
</template>

2,js 部分:设置引入页面路径,

<script>
import customTabbar from '@/components/custom-tabbar/custom-tabbar.vue'
export default {components: {customTabbar} 
}
</script>

3,css 部分

.container {padding-bottom: 50px;/* 适配安全区 */padding-bottom: calc(50px + constant(safe-area-inset-bottom));padding-bottom: calc(50px + env(safe-area-inset-bottom));
}

基本完事,可以试试了。

http://www.lryc.cn/news/621520.html

相关文章:

  • mac 通过homebrew 安装和使用nvm
  • 【uni-app】根据角色/身份切换显示不同的 自定义 tabbar
  • 晶振电路的负载电容、电阻参数设计
  • Vue3 Element-plus 封装Select下拉复选框选择器
  • 一文打通 AI 知识脉络:大语言模型等关键内容详解
  • Docker容器定时任务时区Bug导致业务异常的环境变量配置解决方案
  • Vue3 + Element Plus 实现可搜索、可折叠、可拖拽的部门树组件
  • 【Redis】Redis典型应用——缓存
  • Redis 官方提供免费的 30 MB 云数据库
  • AI客户维护高效解决方案
  • [Chat-LangChain] 前端用户界面 | 核心交互组件 | 会话流管理
  • 制造装配、仓储搬运、快递装卸皆适配!MinkTec 弯曲形变传感器助力,让人体工学改变劳动生活
  • 测试工程师应当具备的能力
  • 专题三_二分_在排序数组中查找元素的第一个和最后一个位置
  • 手机分身空间:空间自由切换,一机体验双重生活!
  • FCC认证三星XR头显加速全球量产,微美全息AI+AR技术引领智能眼镜硬件创新
  • FreeRTOS多核支持
  • PaddleNLP进行Bart文本摘要训练
  • JavaScript 流程控制语句详解
  • 稳定且高效:GSPO如何革新大型语言模型的强化学习训练?
  • SpringCloud -- Nacos详细介绍
  • 跨网络 SSH 访问:借助 cpolar 内网穿透服务实现手机远程管理 Linux
  • 搭建前端开发环境 安装nvm nodejs pnpm 配置环境变量
  • Spark03-RDD01-简介+常用的Transformation算子
  • SQL:生成日期序列(填补缺失的日期)
  • 完整技术栈分享:基于Hadoop+Spark的在线教育投融资大数据可视化分析系统
  • 【Docker】关于hub.docker.com,无法打开,国内使用dockers.xuanyuan.me搜索容器镜像、查看容器镜像的使用文档
  • 关于截屏时实现游戏暂停以及本地和上线不同步问题
  • Java研学-SpringCloud(四)
  • Flink Stream API 源码走读 - keyBy