微信小程序案例 - 自定义 tabBar

一、前言:为什么需要自定义 tabBar?

微信小程序原生 tabBar 虽然简单易用,但存在明显限制:

  • ❌ 不支持中间“+”号等凸起按钮
  • ❌ 图标和文字样式无法高度自定义(如选中态动画)
  • ❌ 无法动态隐藏/显示 tabBar
  • ❌ 不能嵌入徽标(Badge)、红点等业务元素

解决方案使用自定义 tabBar

本文将带你从零实现一个支持中间凸起按钮、带动画、可扩展的自定义 tabBar,并封装为通用组件。


二、最终效果预览

✅ 底部 5 个 tab(中间为“+”发布按钮)
✅ 点击 tab 平滑切换页面
✅ 中间按钮跳转独立功能页(如发布内容)
✅ 支持徽标、选中高亮、图标切换


三、实现原理

由于小程序页面是全屏渲染,我们无法像 H5 那样用 fixed 布局直接覆盖原生 tabBar。

正确做法

  1. 关闭原生 tabBarapp.json 中不配置 tabBar
  2. 每个页面底部手动引入自定义 tabBar 组件
  3. 通过 wx.switchTab 或 wx.navigateTo 模拟 tab 切换
⚠️ 注意:自定义 tabBar 不是全局组件,需在每个 tab 页面中手动引入!

四、第一步:创建自定义 tabBar 组件

1. 目录结构

components/ └── custom-tab-bar/ ├── custom-tab-bar.js ├── custom-tab-bar.json ├── custom-tab-bar.wxml └── custom-tab-bar.wxss

2. 配置组件(custom-tab-bar.json)

{ "component": true, "usingComponents": {} }

3. 定义数据与方法(custom-tab-bar.js)

// components/custom-tab-bar/custom-tab-bar.js Component({ properties: { current: { type: Number, value: 0 } }, data: { // tab 配置(可抽离为常量) tabs: [ { pagePath: "/pages/home/index", text: "首页", icon: "home", selectedIcon: "home-fill" }, { pagePath: "/pages/category/index", text: "分类", icon: "category", selectedIcon: "category-fill" }, { pagePath: "/pages/publish/index", text: "", icon: "add", isCenter: true }, // 中间按钮 { pagePath: "/pages/cart/index", text: "购物车", icon: "cart", selectedIcon: "cart-fill" }, { pagePath: "/pages/my/index", text: "我的", icon: "my", selectedIcon: "my-fill" } ] }, methods: { switchTab(e) { const { index } = e.currentTarget.dataset; const item = this.data.tabs[index]; if (item.isCenter) { // 中间按钮:跳转非 tab 页(如发布页) wx.navigateTo({ url: '/pages/publish/index' }); return; } // 普通 tab:切换页面 wx.switchTab({ url: item.pagePath }); // 可选:触发父页面更新 current this.triggerEvent('change', { index }); } } });
💡 提示:图标建议使用字体图标(如 IconFont)或本地 PNG

4. 编写模板(custom-tab-bar.wxml)

<!-- components/custom-tab-bar/custom-tab-bar.wxml --> <view> <view wx:for="{{tabs}}" wx:key="index"center-btn' : ''}} {{current === index ? 'active' : ''}}" bindtap="switchTab" > <view> <image src="/assets/icons/{{current === index ? item.selectedIcon : item.icon}}.png" mode="aspectFit" /> <!-- 示例:购物车徽标 --> <view wx:if="{{index === 3 && cartCount > 0}}">{{cartCount}}</view> </view> <text wx:if="{{!item.isCenter}}">{{item.text}}</text> </view> </view>

5. 编写样式(custom-tab-bar.wxss)

/* components/custom-tab-bar/custom-tab-bar.wxss */ .tab-bar { position: fixed; bottom: 0; left: 0; right: 0; height: 100rpx; display: flex; justify-content: space-around; align-items: center; background: white; border-top: 1px solid #eee; padding-bottom: env(safe-area-inset-bottom); z-index: 999; } .tab-item { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; position: relative; } .center-btn { position: relative; top: -40rpx; /* 凸起效果 */ width: 120rpx; height: 120rpx; background: #007aff; border-radius: 50%; box-shadow: 0 4rpx 12rpx rgba(0,122,255,0.3); } .center-btn .icon-img { width: 56rpx; height: 56rpx; } .icon-img { width: 48rpx; height: 48rpx; } .tab-text { font-size: 20rpx; margin-top: 8rpx; color: #888; } .active .tab-text { color: #007aff; } .badge { position: absolute; top: -8rpx; right: -8rpx; background: #ff3b30; color: white; border-radius: 50%; min-width: 24rpx; height: 24rpx; font-size: 16rpx; display: flex; align-items: center; justify-content: center; padding: 0 2rpx; }

五、第二步:在页面中使用 tabBar

1. 页面 JSON 引入组件

// pages/home/index.json { "usingComponents": { "custom-tab-bar": "/components/custom-tab-bar/custom-tab-bar" } }

2. 页面 WXML 使用

<!-- pages/home/index.wxml --> <view> <!-- 页面内容 --> <view>首页内容</view> </view> <!-- 底部 tabBar --> <custom-tab-bar current="0" bindchange="onTabChange" />
⚠️ 注意:每个 tab 页面都要引入,并传入对应的 current(0,1,3,4)

3. 页面 JS 处理(可选)

// pages/home/index.js Page({ data: { cartCount: 5 // 示例:从全局状态获取 }, onTabChange(e) { // 如果需要同步 current(一般不需要) console.log('切换到 tab:', e.detail.index); } });

六、关键问题解答

Q1:中间按钮为什么用 navigateTo 而不是 switchTab

A:因为 switchTab 只能跳转到 app.json 中配置的 tabBar 页面,而发布页通常不是 tab 页面,所以用 navigateTo

Q2:如何动态更新徽标(如购物车数量)?

A:可通过全局状态(如 globalData、Event Bus 或 Store)传递数据到 tabBar 组件。

Q3:页面内容被 tabBar 遮挡怎么办?

A:在页面最外层加 padding-bottom: 100rpx + safe-area
.page-container { min-height: 100vh; padding-bottom: calc(100rpx + env(safe-area-inset-bottom)); box-sizing: border-box; }

七、进阶优化建议

  1. 图标使用字体图标:减少图片请求,支持颜色动态修改
  2. 动画效果:点击时添加 scale 动画(transform: scale(0.9)
  3. 状态管理:将 cartCount 等数据通过 Store 统一管理
  4. 适配 iPhone X:使用 env(safe-area-inset-bottom) 避免遮挡

八、结语

感谢您的阅读!如果你有任何疑问或想要分享的经验,请在评论区留言交流!

Read more

AI小白也能快速用五分钟复现的ERNIE-4.5系列模型单卡部署与心理健康机器人实战案例

AI小白也能快速用五分钟复现的ERNIE-4.5系列模型单卡部署与心理健康机器人实战案例

* 本文重点在于文心大模型的微调 * 一起来轻松玩转文心大模型吧👉一文心大模型免费下载地址: https://ai.gitcode.com/theme/1939325484087291906 计算机配置 * 在国内部署选个自带CUDA的会快一点,不自带还得去NVIDIA下载,而其提供的CUDA依赖需要科学上网才能下载快。换阿里清华源也没用。 * 文心模型汇总 环境配置与部署 1. 更换镜像源(使用阿里云镜像源): sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak sudo sed -i 's|http://archive.ubuntu.com/ubuntu|http://mirrors.aliyun.com/ubuntu|g' /etc/apt/sources.

FPGA通信——实现串口通信(Uart)

FPGA通信——实现串口通信(Uart)

一、串口通信介绍 1.1、核心概念 并行通信 (Parallel):像高速公路,8车道同时跑8辆车。速度快,但占用引脚多,且在长距离传输时容易出现“时钟偏差(Skew)”导致数据错位。 串行通信 (Serial):像单行道,车必须一辆接一辆地排队走。引脚少,成本低,且现代高速串行技术(如PCIE, SATA)通过差分信号解决了速度问题。 我们常说的“串口”通常特指 UART (Universal Asynchronous Receiver/Transmitter,通用异步收发传输器)。 1.2、逻辑层面 UART 是一种异步通信协议。 * 异步 (Asynchronous):发送方和接收方之间没有公共的时钟线(不像 SPI 或 I2C 有 CLK 线)。 * 约定:

【Python机器人避障算法实战】:掌握5种核心算法,实现智能路径规划

第一章:Python机器人避障算法概述 在自动化与智能系统领域,机器人避障是实现自主导航的核心能力之一。Python凭借其丰富的库支持和简洁的语法,成为开发机器人避障算法的首选语言。常见的避障策略包括基于传感器的反应式方法(如红外或超声波测距)和基于环境建模的规划算法(如A*、Dijkstra)。这些算法可在仿真环境中验证后部署至实体机器人。 常用避障算法类型 * 人工势场法(APF):将目标点视为引力源,障碍物视为斥力源,通过合力引导机器人移动 * 动态窗口法(DWA):结合机器人的运动学约束,在速度空间中评估可行路径 * 栅格法与A*算法:将环境离散化为网格,搜索从起点到终点的最优路径 传感器数据处理示例 机器人通常依赖传感器获取周围环境信息。以下代码模拟从超声波传感器读取距离并判断是否需要避障: # 模拟超声波传感器输入并触发避障逻辑 def check_obstacle(distance): """ 根据传感器距离判断是否触发避障 :param distance: 当前检测到的前方障碍物距离(单位:厘米) :return: 是否需要避障 """ safe_di

论文阅读:Training language models to follow instructions with human feedback

Ouyang L, Wu J, Jiang X, et al. Training language models to follow instructions with human feedback[J]. Advances in neural information processing systems, 2022, 35: 27730-27744. 引言 引言首先指出了当前大型语言模型(LMs)存在的一个核心问题:模型规模变大并不意味着它们能更好地遵循用户的意图 。具体而言,大型模型经常生成不真实、有毒或对用户毫无帮助的输出,这是因为语言模型的训练目标(预测网页上的下一个 token)与用户希望的目标(“有用且安全地遵循指令”)是错位的。作者的目标是让模型在“有用性”(Helpful)、“诚实性”(Honest)和“无害性”(Harmless)这三个方面与用户意图对齐。