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

相关文章

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图

Python程序打包exe,单文件和多文件方式

《Python程序打包exe,单文件和多文件方式》:本文主要介绍Python程序打包exe,单文件和多文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python 脚本打成exe文件安装Pyinstaller准备一个ico图标打包方式一(适用于文件较少的程

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

Python程序的文件头部声明小结

《Python程序的文件头部声明小结》在Python文件的顶部声明编码通常是必须的,尤其是在处理非ASCII字符时,下面就来介绍一下两种头部文件声明,具有一定的参考价值,感兴趣的可以了解一下... 目录一、# coding=utf-8二、#!/usr/bin/env python三、运行Python程序四、

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

Redis迷你版微信抢红包实战

《Redis迷你版微信抢红包实战》本文主要介绍了Redis迷你版微信抢红包实战... 目录1 思路分析1.1hCckRX 流程1.2 注意点①拆红包:二倍均值算法②发红包:list③抢红包&记录:hset2 代码实现2.1 拆红包splitRedPacket2.2 发红包sendRedPacket2.3 抢