微信小程序自定義tabbar欄實(shí)現(xiàn)過程講解
前言
昨天主管突然給我說微信小程序默認(rèn)的 tabBar
不美觀,讓我改成中間突出的那種樣式。縱然我心里面有千般不情愿,但還是接下了這個(gè)任務(wù)。查了一下文檔 自定義 tabBar 發(fā)現(xiàn)有這個(gè)方法,有思路了就趕緊搞起來,以下是我的開發(fā)經(jīng)驗(yàn)分享。
一、自定義tabbar欄配置
- 在 app.json 文件中的 tabBar 中指定 custom 字段為 true(意思是允許使用自定義 tabBar);
- 在 app.json 中全局開啟使用組件,或者在所有涉及的 tab 頁 json 中申明usingComponents 項(xiàng);
- 在 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)建方式需要自己改動(dòng)一些代碼,在這里我們選用新建 Component
的方式。
添加組件代碼
1、完善 wxml
文件代碼,tabBar
欄需要展示的頁面是一個(gè)固定的數(shù)組,可以使用 wx:for
循環(huán)展示,在這里用到 selected
這個(gè)字段,這個(gè)字段的作用是幫助展示 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)建全局字段
做完以上工作之后,我們可以就可以看一下效果了,是不是就以為這樣就可以了呢?但是事與愿違,會(huì)發(fā)現(xiàn)這里存在 bug
,在我們點(diǎn)擊 tabBar
欄進(jìn)行跳轉(zhuǎn)的時(shí)候會(huì)發(fā)現(xiàn)頁面跳轉(zhuǎn)過去了,但是對(duì)應(yīng)頁面的 tabBar
沒有改變顏色。為了解決這個(gè) bug
,需要添加全局字段。在 app.js 文件中創(chuàng)建該字段。
globalData: { selected: 0 },
在組件中保存重要字段
全局字段創(chuàng)建完成之后,我們需要在組件 js 文件中使用該字段,在 ready
函數(shù)中保存這個(gè)字段,在點(diǎn)擊 tabBar
欄時(shí),把相應(yīng)的index
賦值給這個(gè)全局字段。
// 引入全局函數(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}) } } })
添加完成后,可以測(cè)試一下效果,發(fā)現(xiàn)剛才的 bug
已經(jīng)解決。perfect?。?/code>
三、效果展示
到此這篇關(guān)于微信小程序自定義tabbar欄實(shí)現(xiàn)過程講解的文章就介紹到這了,更多相關(guān)小程序tabbar欄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用proxy實(shí)現(xiàn)一個(gè)簡(jiǎn)單完整的MVVM庫的示例代碼
這篇文章主要介紹了如何使用proxy實(shí)現(xiàn)一個(gè)簡(jiǎn)單完整的MVVM庫的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09JavaScript中檢測(cè)數(shù)據(jù)類型的四種方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了四個(gè)JavaScript中檢測(cè)數(shù)據(jù)類型的常用方法,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以參考一下2023-04-04js實(shí)現(xiàn)動(dòng)畫特效的文字鏈接鼠標(biāo)懸停提示的方法
這篇文章主要介紹了js實(shí)現(xiàn)動(dòng)畫特效的文字鏈接鼠標(biāo)懸停提示的方法,實(shí)例分析了javascript操作css的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03一文搞懂JavaScript如何實(shí)現(xiàn)圖片懶加載
圖片懶加載,往往作為減少首頁白屏?xí)r間的一個(gè)解決方案而出現(xiàn)。本文將通過示例帶大家一起探究一下JavaScript是如何實(shí)現(xiàn)圖片懶加載的,感興趣的可以了解一下2022-06-06javascript?實(shí)現(xiàn)純前端將數(shù)據(jù)導(dǎo)出excel兩種方式
這篇文章主要介紹了javascript?實(shí)現(xiàn)純前端將數(shù)據(jù)導(dǎo)出excel兩種方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參一下2022-07-07