微信小程序自定义 底部 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 (中间凸起)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/308607

相关文章

微信公众号脚本-获取热搜自动新建草稿并发布文章

《微信公众号脚本-获取热搜自动新建草稿并发布文章》本来想写一个自动化发布微信公众号的小绿书的脚本,但是微信公众号官网没有小绿书的接口,那就写一个获取热搜微信普通文章的脚本吧,:本文主要介绍微信公众... 目录介绍思路前期准备环境要求获取接口token获取热搜获取热搜数据下载热搜图片给图片加上标题文字上传图片

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A