当前位置:网站首页>Wechat applet custom tabbar
Wechat applet custom tabbar
2022-04-21 22:18:00 【inticaler】
Realization effect :

Official documents :
Customize tabBar | Wechat open documents
Implementation steps :
1. stay app.json Medium tabBar Item designation custom Field , At the same time, the rest tabBar Relevant configurations are also supplemented .
// You need to define tabBar page
// “pages” Don't forget the configuration
"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": " home page "
},
{
"pagePath": "pages/bill/index",
"iconPath": "static/images/tabbar/bill.png",
"selectedIconPath": "static/images/tabbar/bill_cur.png",
"text": " bill "
},
{
"pagePath": "pages/plan/index",
"iconPath": "static/images/tabbar/plan.png",
"selectedIconPath": "static/images/tabbar/plan_cur.png",
"text": " plan "
},
{
"pagePath": "pages/mine/index",
"iconPath": "static/images/tabbar/mine.png",
"selectedIconPath": "static/images/tabbar/mine_cur.png",
"text": " my "
}
]
},
2. Create a new directory in the root directory custom-tab-bar Folder ( Bear in mind , Folder name cannot be changed , Otherwise, we can't get ),
#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);
/*( Compatible with mobile phones with security zones )*/
padding-bottom: constant(safe-area-inset-bottom);/* compatible IOS<11.2*/
padding-bottom: env(safe-area-inset-bottom);/* compatible 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',
// Ready to customize tabbar resources
// Self defined tabBar Need to be in app.json In the configuration
tabbarList: [
{
"pagePath": "/pages/home/index",
"iconPath": "/static/images/tabbar/home.png",
"selectedIconPath": "/static/images/tabbar/home_cur.png",
"text": " home page ",
"name": "home"
},
{
"pagePath": "/pages/bill/index",
"iconPath": "/static/images/tabbar/bill.png",
"selectedIconPath": "/static/images/tabbar/bill_cur.png",
"text": " bill ",
"name": "bill"
},
{
"pagePath": "/pages/plan/index",
"iconPath": "/static/images/tabbar/plan.png",
"selectedIconPath": "/static/images/tabbar/plan_cur.png",
"text": " plan ",
"name": "plan"
},
{
"pagePath": "/pages/mine/index",
"iconPath": "/static/images/tabbar/mine.png",
"selectedIconPath": "/static/images/tabbar/mine_cur.png",
"text": " my ",
"name": "mine"
}
]
},
lifetimes:{
// Component lifecycle
attached(){
// Execute when the component real column enters the page node tree
console.log(' well , I was executed ');
}
},
methods:{
// Component defined methods ( Strong visual sense )
swicthTabbar(e){
let { index} = e.currentTarget.dataset;
wx.switchTab({
url: this.data.tabbarList[index].pagePath,
})
}
}
})
3. stay app.js Define global in setTabbar Method
App({
// Define global setTabbar Method
$setTabbar(that, currentName){
let tabbar = that.getTabBar();
console.log(tabbar);
tabbar.setData({
currentName
})
},
onLaunch() {
},
})
4. In the use of tabbar Add and change the method of the currently selected page ( for example :home.js,bill.js,plan.js,mine.js)
onShow() {
//'home' According to the page In the following tabbarList Of name Property requires
getApp().$setTabbar(this, 'home');
},
5. In addition, you need to use tabbar Your page needs to consider tabbar Height ( That's how I handled it , Customize a placeholder )
<view class="plan">
<!-- Content -->
<view class="content"></view>
<!-- Place holder -->
<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;
/*( Compatible with mobile phones with security zones )*/
padding-bottom: constant(safe-area-inset-bottom);/* compatible IOS<11.2*/
padding-bottom: env(safe-area-inset-bottom);/* compatible IOS>11.2*/
}
.content {
overflow: hidden;
flex: 1;
}
thus , Customization of wechat applet tabbar It's done.
版权声明
本文为[inticaler]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212212209302.html
边栏推荐
猜你喜欢

It is revealed that Xiaomi has new machines this month, and many of its products are ready to go

攻防世界 mfw

每日练题(3)
![[MySQL] solve the problem of MAC accessing MySQL server on windows](/img/45/53ef2fe3abed114148340787226260.png)
[MySQL] solve the problem of MAC accessing MySQL server on windows

Building local canal middleware for data migration -- Inspiration from cache breakdown

Kotlin crawler app, Android development, interview preparation

ROS robot from starting point to end point (IV) reproduction of blue bridge cloud practice

【Canvas】基础绘制与使用

LeetCode146-LRU缓存-模拟-双向链表-哈希表-数据结构-操作系统

解锁OpenHarmony技术日!年度盛会,即将揭幕!
随机推荐
树莓派3B+ 安装MJPG-streamer
CSR8670 spi方式软件烧录方法
CC00000. ZABBIX———————————————
Oracle cascade delete table (not subject to foreign key constraints)
It is revealed that Xiaomi has new machines this month, and many of its products are ready to go
[ES6] function extension
Number to thousand separator number
【ES6】Generator
软件设计师——第六章:系统安全分析与设计
How can "Xiaodeng" enterprises solve the problem of weak password in AD domain?
File create file problem
Do you have any good suggestions for brushing questions and how to improve efficiency?
[ES6] array expansion
Echart writes a large screen showing a circular edge gradient histogram
排序方式(8种)详解6—快速排序
Software life cycle
[ES6] iterator and forof loop
OpenCV中的Core组件——输入输出XML, YAML(12)
专精特新代办机构及专精特新代办政策分析解读,补贴20-100万
Overview of MySQL database paradigm design theory