微信小程序自定義tabbar實現(xiàn)突出樣式詳解流程
前言
昨天主管突然給我說微信小程序默認(rèn)的 tabBar 不美觀,讓我改成中間突出的那種樣式。縱然我心里面有千般不情愿,但還是接下了這個任務(wù)。查了一下文檔 自定義 tabBar 發(fā)現(xiàn)有這個方法,有思路了就趕緊搞起來,以下是我的開發(fā)經(jīng)驗分享。
一、自定義tabbar欄配置
- 在 app.json 文件中的 tabBar 中指定 custom 字段為 true(意思是允許使用自定義 tabBar);
- 在 app.json 中全局開啟使用組件,或者在所有涉及的 tab 頁 json 中申明usingComponents 項;
- 在 app.json 中添加作為 tabBar 欄的頁面;
示例代碼
"tabBar": {
"custom": true,
"color": "#afafaf",
"selectedColor": "#0099f6",
"backgroundColor": "#F7F8F8",
"list": [
{
"pagePath": "pages/index/index",
"text": "首頁"
},
{
"pagePath": "pages/goodIndexCopy/goodIndexCopy",
"text": "易購商城"
},
{
"pagePath": "pages/release/release",
"text": "發(fā)布"
},
{
"pagePath": "pages/nearby/nearby",
"text": "本地"
},
{
"pagePath": "pages/mine/mine",
"text": "我的"
}
]
},
"usingComponents": {},
pagePath 是自己添加的頁面,text 是tabBar上展示的文字。
二、添加自定義tabbar欄組件
在根目錄下創(chuàng)建 custom-tab-bar 文件夾,并在該文件夾下新建 Component,或者新建 Page,但是這種創(chuàng)建方式需要自己改動一些代碼,在這里我們選用新建 Component 的方式。
添加組件代碼
1、完善 wxml 文件代碼,tabBar 欄需要展示的頁面是一個固定的數(shù)組,可以使用 wx:for 循環(huán)展示,在這里用到 selected 這個字段,這個字段的作用是幫助展示 tabBar 選中和未選中的樣式。
<!--custom-tab-bar/index.wxml-->
<view class="tab-bar">
<view wx:for="{{list}}" wx:key="index" class="tab-bar-item {{item.bulge?'bulge':''}}" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
<view wx:if="item.bulge" class="tab-bar-bulge"></view>
<image class="image" src="{{selected == index ? item.selectedIconPath : item.iconPath}}"></image>
<!-- <view wx:if="{{item.text}}" style="color: {{selected === index ? selectedColor : color}}" class="tab-bar-view">{{item.text}}</view> -->
<view class="tab-bar-view" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
</view>
</view>
2、完善 js 文件代碼,list 數(shù)組就是在 tabBar 欄展示的頁面信息,switchTab 方法作用可以出看來是負(fù)責(zé)跳轉(zhuǎn)頁面。其它的字段相信各位都知道,這里就不再描述。
/**
* 組件的初始數(shù)據(jù)
*/
data: {
selected: 0,
color: "#afafaf",
selectedColor: "#0099f6",
backgroundColor: "#F7F8F8",
list: [
{
pagePath: "/pages/index/index",
iconPath: "/images/icon/wtc/icon_zhuye2.png",
selectedIconPath: "/images/icon/wtc/icon_zhuye2_d.png",
text: "首頁",
},
{
pagePath: "/pages/goodIndexCopy/goodIndexCopy",
iconPath: "/images/icon/wtc/icon_pintuan2.png",
selectedIconPath: "/images/icon/wtc/icon_pintuan2_d.png",
text: "易購商城"
},
{
pagePath: "/pages/release/release",
bulge:true,
iconPath: "/images/add.png",
selectedIconPath: "/images/add-active.png",
text: "發(fā)布"
},
{
pagePath: "/pages/nearby/nearby",
iconPath: "/images/icon/wtc/icon_pintuan3.png",
selectedIconPath: "/images/icon/wtc/icon_pintuan3_d.png",
text: "本地",
},
{
pagePath: "/pages/mine/mine",
iconPath: "/images/icon/wtc/icon_wode3.png",
selectedIconPath: "/images/icon/wtc/icon_wode3_d.png",
text: "我的"
},
]
},
/**
* 組件的方法列表
*/
methods: {
switchTab(e) {
// console.log(e);
const data = e.currentTarget.dataset;
const url = data.path;
wx.switchTab({url})
}
}
3、完善 wxss 文件代碼。
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 50px;
background: #FFF;
display: flex;
line-height: 1.2;
padding-bottom: env(safe-area-inset-bottom);
border-top: 1px solid #e6e6e6;
}
.tab-bar-item {
flex: 1;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.tab-bar-item .image {
width: 26px;
height: 26px;
}
.bulge {
background-color: #FFF;
}
.bulge .tab-bar-bulge{
position: absolute;
z-index: -1;
width: 64px;
height: 64px;
top: -24px;
border-radius: 50%;
border-top: 1px solid #e6e6e6;
background-color: #FFF;
}
.bulge .image{
position: absolute;
width: 50px;
height: 50px;
top: -20px;
}
.bulge .tab-bar-view{
position: relative;
bottom: -16px;
margin-top: 4px;
}
.tab-bar-item .tab-bar-view {
font-size: 12px;
margin-top: 4px;
}
創(chuàng)建全局字段
做完以上工作之后,我們可以就可以看一下效果了,是不是就以為這樣就可以了呢?但是事與愿違,會發(fā)現(xiàn)這里存在 bug,在我們點擊 tabBar 欄進行跳轉(zhuǎn)的時候會發(fā)現(xiàn)頁面跳轉(zhuǎn)過去了,但是對應(yīng)頁面的 tabBar 沒有改變顏色。為了解決這個 bug,需要添加全局字段。在 app.js 文件中創(chuàng)建該字段。
globalData: {
selected: 0
},
在組件中保存重要字段
全局字段創(chuàng)建完成之后,我們需要在組件 js 文件中使用該字段,在 ready 函數(shù)中保存這個字段,在點擊 tabBar 欄時,把相應(yīng)的index 賦值給這個全局字段。
// 引入全局函數(shù)
const app = getApp()
Component({
/**
* 組件的初始數(shù)據(jù)
*/
data: {
selected: 0,
color: "#afafaf",
selectedColor: "#0099f6",
backgroundColor: "#F7F8F8",
list: [
{
pagePath: "/pages/index/index",
iconPath: "/images/icon/wtc/icon_zhuye2.png",
selectedIconPath: "/images/icon/wtc/icon_zhuye2_d.png",
text: "首頁",
},
{
pagePath: "/pages/goodIndexCopy/goodIndexCopy",
iconPath: "/images/icon/wtc/icon_pintuan2.png",
selectedIconPath: "/images/icon/wtc/icon_pintuan2_d.png",
text: "易購商城"
},
{
pagePath: "/pages/release/release",
bulge:true,
iconPath: "/images/add.png",
selectedIconPath: "/images/add-active.png",
text: "發(fā)布"
},
{
pagePath: "/pages/nearby/nearby",
iconPath: "/images/icon/wtc/icon_pintuan3.png",
selectedIconPath: "/images/icon/wtc/icon_pintuan3_d.png",
text: "本地",
},
{
pagePath: "/pages/mine/mine",
iconPath: "/images/icon/wtc/icon_wode3.png",
selectedIconPath: "/images/icon/wtc/icon_wode3_d.png",
text: "我的"
},
]
},
ready: function() {
this.setData({
selected: app.globalData.selected
})
},
/**
* 組件的方法列表
*/
methods: {
switchTab(e) {
// console.log(e);
const data = e.currentTarget.dataset;
const url = data.path;
app.globalData.selected = data.index;
wx.switchTab({url})
}
}
})
添加完成后,可以測試一下效果,發(fā)現(xiàn)剛才的 bug 已經(jīng)解決。perfect??!
三、效果展示

到此這篇關(guān)于微信小程序自定義tabbar實現(xiàn)突出樣式詳解流程的文章就介紹到這了,更多相關(guān)小程序自定義tabbar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js事件on動態(tài)綁定數(shù)據(jù),綁定多個事件的方法
今天小編就為大家分享一篇js事件on動態(tài)綁定數(shù)據(jù),綁定多個事件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
值得學(xué)習(xí)的JavaScript調(diào)試技巧分享
這篇文章主要給大家介紹了一些JavaScript調(diào)試技巧,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
微信小程序?qū)崿F(xiàn)MUI數(shù)字輸入框效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)MUI數(shù)字輸入框效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
基于JS實現(xiàn)Flappy?Bird游戲的示例代碼
Flappy?Bird是13年紅極一時的小游戲,即摁上鍵控制鳥的位置穿過管道間的縫隙。本文將用JS實現(xiàn)這一經(jīng)典的游戲,需要的可以參考一下2022-04-04

