当前位置:网站首页>微信小程序自定义tabbar
微信小程序自定义tabbar
2022-04-21 22:12:00 【inticaler】
实现效果:

官方文档:
实现步骤:
1. 在 app.json 中的 tabBar 项指定 custom 字段,同时其余 tabBar 相关配置也补充完整。
// 需要先定义tabBar页面
// “pages” 配置里面也不要忘了
"tabBar": {
"custom": true,
"color": "#999999",
"selectedColor": "#eb7209",
"backgroundColor": "#ffffff",
"list": [
{
"pagePath": "pages/home/index",
"iconPath": "static/images/tabbar/home.png",
"selectedIconPath": "static/images/tabbar/home_cur.png",
"text": "首页"
},
{
"pagePath": "pages/bill/index",
"iconPath": "static/images/tabbar/bill.png",
"selectedIconPath": "static/images/tabbar/bill_cur.png",
"text": "单据"
},
{
"pagePath": "pages/plan/index",
"iconPath": "static/images/tabbar/plan.png",
"selectedIconPath": "static/images/tabbar/plan_cur.png",
"text": "计划"
},
{
"pagePath": "pages/mine/index",
"iconPath": "static/images/tabbar/mine.png",
"selectedIconPath": "static/images/tabbar/mine_cur.png",
"text": "我的"
}
]
},
2. 在根目录新建custom-tab-bar文件夹(切记,文件夹名称不能更改,不然获取不到),
#index.html
<!--custom-tab-bar/index.wxml-->
<cover-view class="custom-tabbar">
<cover-view wx:for="{
{tabbarList}}" wx:key="index" class="tabbar-item" data-index="{
{index}}" bindtap="swicthTabbar">
<cover-image src="{
{currentName == item.name ? item.selectedIconPath : item.iconPath}}" class="tabbar-icon" style="{
{ item.style }}"></cover-image>
<cover-view class="{
{currentName == item.name ? 'text-active' : 'tabbar-text'}}">{
{ item.text }}</cover-view>
</cover-view>
</cover-view>
#index.wxss
/* custom-tab-bar/index.wxss */
.custom-tabbar{
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 112rpx;
background-color: #fff;
box-shadow: 0px 6px 12px rgba(0, 0, 0, 0.24);
/*(兼容有安全区域的手机)*/
padding-bottom: constant(safe-area-inset-bottom);/*兼容 IOS<11.2*/
padding-bottom: env(safe-area-inset-bottom);/*兼容 IOS>11.2*/
display: flex;
}
.tabbar-item{
flex: 1;
height: 100%;
display: flex;
justify-content:center;
align-items: center;
flex-direction: column;
font-size: 20rpx;
}
.tabbar-icon{
width: 40rpx;
height: 40rpx;
margin-bottom: 8rpx;
}
.tabbar-text{
color: #999999;
}
.text-active{
color: #EB7209;
}
#index.js
// custom-tab-bar/index.js
Component({
data:{
currentName: 'home',
//准备好自定义tabbar资源
//自定义的tabBar 需要在app.json配置中存在
tabbarList: [
{
"pagePath": "/pages/home/index",
"iconPath": "/static/images/tabbar/home.png",
"selectedIconPath": "/static/images/tabbar/home_cur.png",
"text": "首页",
"name": "home"
},
{
"pagePath": "/pages/bill/index",
"iconPath": "/static/images/tabbar/bill.png",
"selectedIconPath": "/static/images/tabbar/bill_cur.png",
"text": "单据",
"name": "bill"
},
{
"pagePath": "/pages/plan/index",
"iconPath": "/static/images/tabbar/plan.png",
"selectedIconPath": "/static/images/tabbar/plan_cur.png",
"text": "计划",
"name": "plan"
},
{
"pagePath": "/pages/mine/index",
"iconPath": "/static/images/tabbar/mine.png",
"selectedIconPath": "/static/images/tabbar/mine_cur.png",
"text": "我的",
"name": "mine"
}
]
},
lifetimes:{
//组件生命周期
attached(){
//在组件实列进入页面节点树时执行
console.log('嘿,我被执行了');
}
},
methods:{
//组件定义的方法(浓浓的既视感)
swicthTabbar(e){
let { index} = e.currentTarget.dataset;
wx.switchTab({
url: this.data.tabbarList[index].pagePath,
})
}
}
})
3.在app.js中定义全局setTabbar方法
App({
//定义全局setTabbar方法
$setTabbar(that, currentName){
let tabbar = that.getTabBar();
console.log(tabbar);
tabbar.setData({
currentName
})
},
onLaunch() {
},
})
4.在用到tabbar的页面添加更改当前选中页面方法(例如:home.js,bill.js,plan.js,mine.js)
onShow() {
//'home' 根据页面来 在下面的tabbarList的name属性需要用到
getApp().$setTabbar(this, 'home');
},
5.另外需要用到tabbar的页面需要考虑tabbar的高度(我是这样做处理的,自定义一个占位符)
<view class="plan">
<!-- 内容 -->
<view class="content"></view>
<!-- 占位符 -->
<view class="plan-block"></view>
</view>
.plan {
height: 100%;
padding: 0 40rpx;
display: flex;
flex-direction: column;
}
.plan .plan-block {
box-sizing: content-box;
height: 112rpx;
/*(兼容有安全区域的手机)*/
padding-bottom: constant(safe-area-inset-bottom);/*兼容 IOS<11.2*/
padding-bottom: env(safe-area-inset-bottom);/*兼容 IOS>11.2*/
}
.content {
overflow: hidden;
flex: 1;
}
至此,微信小程序的自定义tabbar就完成了
版权声明
本文为[inticaler]所创,转载请带上原文链接,感谢
https://blog.csdn.net/MICHAEL_PRINCE/article/details/124298357
边栏推荐
- CV战神常用代码-----kj15
- Hospital-Oriented RFID Service
- 【WebGIS】WebGIS、桌面GIS、移动GIS、三维GIS的简介
- Jupyter notebook has no run button
- 数据库事务学习总结
- [vscode] detailed user manual for debugger
- 字节日常实习(已OC)
- ROS robot from starting point to end point (IV) reproduction of blue bridge cloud practice
- Oracle cascade delete table (not subject to foreign key constraints)
- Thread safety for the first time. This article is enough
猜你喜欢

Restcloud ETL out of the box - permanently free

WPF data-driven method for modifying binding

Idea operates redis on Linux through jedis; Failed to connect to any host resolved for DNS name

刷题有什么好的建议吗,怎么样可以提升效率?

软件的生命周期

Attack and defense world MFW

Kotlin core programming, Android development, interview answer handler

Smart Chemical Park solutions

字节日常实习(已OC)

【Canvas】基础绘制与使用
随机推荐
Gd32f103 learning notes (8) -- use of ADC interface
Software testing process and testing model
Interview must brush algorithm top101 knapsack nine lectures Top13
INT 102_TTL 09
Attack and defense world MFW
Start debugging the dynamic linker of C Library-2 -- musl-c
How does wechat applet realize the function of jumping from commodity list to commodity details page
Because the epidemic makes the garment industry fully realize the necessity of digital transformation.
【Pinia】第二章 核心概念
搭建本地Canal中间件进行数据迁移 —— 从缓存击穿得到的启发
Intensive reading of Fanfan's anti attack paper (II) CVPR 2021 yuan learning training simulator for ultra efficient black box attack (Tsinghua)
字节日常实习(已OC)
Online yaml to properties tool
CPT 102_LEC 11
Building local canal middleware for data migration -- Inspiration from cache breakdown
汇编编写中断
AutoCAD -- drawing method of three kinds of arrows
CPT 102_ LEC 11
kotlin爬虫app,Android开发面试准备
正则表达式