微信小程序自定义 底部 tabbar (中间凸起)

2023-10-30 14:40

本文主要是介绍微信小程序自定义 底部 tabbar (中间凸起),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、新建一个 tabbar 组件

1.新建文件

在与 pages 文件夹同级的地方新建 custom-tab-bar 文件夹,并新建 index.wxml 、index.wxss 、index.js 、index.json 四个文件夹
在这里插入图片描述

2.index.wxml

<view class="tab-bar"><view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab"><view class="center_part"><view class="center_part_code" wx:if="{{index == 2}}"><image class=" center-has-noimg" src="{{centerImg}}"></image></view><image class=" center-has-image" src="{{selected === index ? item.selectedIconPath : item.iconPath}}" wx:else></image></view><view style="color: {{selected === index ? selectedColor : color}}" class="cover-text">{{item.text}}</view><view class="number" wx-if="{{item.number != ''}}">{{item.number}}</view></view><view class="safeArea"></view>
</view>

3.index.wxss

.tab-bar {position: fixed;bottom: 0;left: 0;right: 0;height: 100rpx;display: flex;padding-bottom: env(safe-area-inset-bottom);z-index: 99;padding-top: 17rpx;background: url("https://ns51.eaia.cn/group1/M00/01/E4/wKgKymLgpdWARzR-AAANbZWJ5GM302.png")no-repeat;background-size: 100vw auto;
}.tab-bar-item {flex: 1;text-align: center;display: flex;justify-content: center;align-items: center;flex-direction: column;
}
.tab-bar-item cover-image {width: 27px;height: 27px;
}.tab-bar-item .cover-text {font-size: 10px;
}
.txt {color: #aaaaaa;
}
.fontWeight {font-weight: bold;
}
.bg_rec {background: #ffd324;width: 80rpx;min-height: auto;height: 20rpx;margin-top: -28rpx;vertical-align: text-bottom;border-radius: 0;z-index: -10;
}
.center_img {width: 100rpx;height: 100rpx;transform: translate(-50%);left: 50%;bottom: 0;
}
.center-has-noimg {width: 100%;height: 100%;
}
.center-has-image {width: 38rpx;height: 38rpx;
}
.center_part_code {padding: 15rpx;box-shadow: 0 0 0 #000;/* width: 100rpx;height: 100rpx; */position: absolute;top: -22px;z-index: 10;width: 90rpx;height: 90rpx;transform: translate(-50%);left: 50%;box-sizing: border-box;border-radius: 50%;background-color: #1296db;
}
.tab-bar-item:nth-child(3) > .cover-text {position: absolute;/* bottom: 4rpx; */bottom: calc(14rpx + env(safe-area-inset-bottom));
}
.number {position: absolute;font-size: 24rpx;min-width: 40rpx;min-height: 40rpx;display: flex;justify-content: center;align-items: center;color: #fff;padding: 4rpx;line-height: 24rpx;background-color: #fd6334;border-radius: 50%;transform: translate(26rpx, -40rpx);
}.safeArea {position: absolute;bottom: 0;left: 0;width: 100%;height: calc(6rpx + env(safe-area-inset-bottom));background-color: #fff;
}

4.index.js

注意路径!!!

Component({data: {selected: 0,color: "#7A7E83",selectedColor: "#1296db",list: [{pagePath: "/pages/index/index",text: "首页",iconPath: "/static/images/tabbar/home.png",selectedIconPath: "/static/images/tabbar/home-active.png",number: "",},{pagePath: "/pages/goods/goods",text: "商品",iconPath: "/static/images/tabbar/goods.png",selectedIconPath: "/static/images/tabbar/goods-active.png",number: "",},{pagePath: "/pages/logs/logs",text: "",iconPath: "/static/images/tabbar/home.png",selectedIconPath: "/static/images/tabbar/home-active.png",number: "",},{pagePath: "/pages/notice/notice",text: "消息",iconPath: "/static/images/tabbar/test.png",selectedIconPath: "/static/images/tabbar/test-active.png",number: "",},{pagePath: "/pages/mine/mine",text: "我的",iconPath: "/static/images/tabbar/mine.png",selectedIconPath: "/static/images/tabbar/mine-active.png",number: "",},],// background: '../static/tabbar-bg.png',centerImg: "/static/images/tabbar/cart.png",},// attached() {//     如果中间凸起跳转 tabbar 页面,则把下面注释打开//     if (this.data.selected == 2) {//         this.setData({//             centerImg: '/static/icon/xinjian.png'//         })//     } else {//         this.setData({//             centerImg: '/static/icon/xinjian.png'//         })//     }//     显示数字提示//     let numi = 'list[3].number'//     this.setData({//         [numi]: 12//     })// },methods: {// 切换 tab 事件switchTab(e) {const data = e.currentTarget.dataset;const path = data.path;const index = data.index;// 跳转页面if (index != 2) {wx.switchTab({url: path,});} else {//中间凸起跳转非tabbar//注意,如果跳转tabbar页面则不进行判断wx.navigateTo({url: "/pages/cart/cart",});}},},
});

5.index.json

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

二、配置 tabbar

1.app.json

复制后会报错,把图片和页面路径改掉就好了!!!

{"tabBar": {"custom": true, // 一定要写这个!!!"color": "#000","selectedColor": "#1296DB","list": [{"pagePath": "pages/index/index","text": "首页","iconPath": "/static/images/tabbar/home.png","selectedIconPath": "/static/images/tabbar/home-active.png"},{"pagePath": "pages/goods/goods","text": "商品","iconPath": "/static/images/tabbar/home.png","selectedIconPath": "/static/images/tabbar/home-active.png"},{"pagePath": "pages/logs/logs","text": "","iconPath": "/static/images/tabbar/home.png","selectedIconPath": "/static/images/tabbar/home-active.png"},{"pagePath": "pages/notice/notice","text": "消息","iconPath": "/static/images/tabbar/test.png","selectedIconPath": "/static/images/tabbar/test-active.png"},{"pagePath": "pages/mine/mine","text": "我的","iconPath": "/static/images/tabbar/mine.png","selectedIconPath": "/static/images/tabbar/mine-active.png"}]}
}

2.在 tabbar 页面中设置

onShow() {if (typeof this.getTabBar === "function" && this.getTabBar()) {this.getTabBar().setData({// 这里的数字对应tabbar对应的索引selected: 4,});}
},

三、效果图

在这里插入图片描述


总结

提示:

不要无脑复制,复制到自己的项目中后记得更改图片、页面路径!!!
如需自定义 头部 navbar 请点击这里

这篇关于微信小程序自定义 底部 tabbar (中间凸起)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/weixin_72228920/article/details/131462077
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/308607

相关文章

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

基于Python编写自动化邮件发送程序(进阶版)

《基于Python编写自动化邮件发送程序(进阶版)》在数字化时代,自动化邮件发送功能已成为企业和个人提升工作效率的重要工具,本文将使用Python编写一个简单的自动化邮件发送程序,希望对大家有所帮助... 目录理解SMTP协议基础配置开发环境构建邮件发送函数核心逻辑实现完整发送流程添加附件支持功能实现htm

C#控制台程序同步调用WebApi实现方式

《C#控制台程序同步调用WebApi实现方式》控制台程序作为Job时,需同步调用WebApi以确保获取返回结果后执行后续操作,否则会引发TaskCanceledException异常,同步处理可避免异... 目录同步调用WebApi方法Cls001类里面的写法总结控制台程序一般当作Job使用,有时候需要控制

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序