微信小程序案例 - 自定义 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

6.llamafactory项目介绍与安装部署

6.llamafactory项目介绍与安装部署

一、学术资源加速 * 服务说明:AutoDL提供学术资源加速服务,主要解决GitHub和HuggingFace访问速度慢的问题,但仅限学术用途且不承诺稳定性 * 加速地址:包含github.com、githubusercontent.com、githubassets.com、huggingface.co等域名 * 终端配置: * 注意事项: * 建议不需要时关闭加速,可能影响正常网络 * 关闭命令: 二、主流微调框架介绍 1. Transformer * 生态地位:Hugging Face核心库,NLP领域最广泛使用的基础框架 * 技术特点: * 支持全参数微调 * 兼容PEFT库扩展 * 优势: * 生态系统最完善,社区活跃 * 与PyTorch/TensorFlow无缝集成 * 模型和教程资源丰富 * 适用场景:中小规模模型实验、研究和开发,微调入门首选 2. PEFT * 技术定位:参数高效微调标准库 * 核心方法: * LoRA * Prefix-tuning * AdaLoRA

8卡RTX 5090服务器llama.cpp测试

8 卡 RTX 5090 服务器 完整安装及性能调优指南  8卡RTX 5090服务器 从 NVIDIA驱动安装 → CUDA环境 → llama.cpp编译 → 多GPU测试 的完整、可直接执行流程(基于Ubuntu 22.04 LTS,适配Blackwell架构)。 一、系统与硬件准备(必做) 1.1 系统要求 • 推荐:Ubuntu 22.04 LTS(64位) • 内核:6.8+ HWE内核(5090必须高内核) • 禁用:Nouveau开源驱动(与NVIDIA驱动冲突) 1.2 硬件检查 Bash # 查看8张5090是否被识别 lspci | grep -i nvidia

【GitHub】github学生认证,在vscode中使用copilot的教程

【GitHub】github学生认证,在vscode中使用copilot的教程

github学生认证并使用copilot教程 * 写在最前面 * 一.注册github账号 * 1.1、注册 * 1.2、完善你的profile * 二、Github 学生认证 * 注意事项:不完善的说明 * 三、Copilot * 四、在 Visual Studio Code 中安装 GitHub Copilot 扩展 * 4.1 安装 Copilot 插件 * 4.2 配置 Copilot 插件(新安装) * 4.3 换 Copilot 插件账号 🌈你好呀!我是 是Yu欸🌌 2024每日百字篆刻时光,感谢你的陪伴与支持 ~🚀 欢迎一起踏上探险之旅,挖掘无限可能,共同成长!

在Windows11利用llama.cpp调用Qwen3.5量化模型测试

1.下载llama.cpp二进制文件 访问 https://github.com/ggml-org/llama.cpp/releases 或者 https://bgithub.xyz/ggml-org/llama.cpp/releases 选择适合自己平台的。我没有独立显卡,所以选择CPU版本 https://bgithub.xyz/ggml-org/llama.cpp/releases/download/b8192/llama-b8192-bin-win-cpu-x64.zip 解压到\d\llama8\目录。 2.下载量化模型 按照 章北海mlpy 公众号:Ai学习的老章~ID:mindszhang666 写的知乎文章Qwen3.5 0.8B/2B/