微信小程序自定義tabbar實現(xiàn)突出樣式詳解流程
前言
昨天主管突然給我說微信小程序默認的 tabBar
不美觀,讓我改成中間突出的那種樣式??v然我心里面有千般不情愿,但還是接下了這個任務。查了一下文檔 自定義 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
方法作用可以出看來是負責跳轉(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)過去了,但是對應頁面的 tabBar
沒有改變顏色。為了解決這個 bug
,需要添加全局字段。在 app.js 文件中創(chuàng)建該字段。
globalData: { selected: 0 },
在組件中保存重要字段
全局字段創(chuàng)建完成之后,我們需要在組件 js 文件中使用該字段,在 ready
函數(shù)中保存這個字段,在點擊 tabBar
欄時,把相應的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??!
三、效果展示
到此這篇關于微信小程序自定義tabbar實現(xiàn)突出樣式詳解流程的文章就介紹到這了,更多相關小程序自定義tabbar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
js defineSetter -給js的 "class"自動增加一個set的屬性(方法)
js defineSetter -給js的 "class"自動增加一個set的屬性(方法)...2007-06-06js操作textarea方法集合封裝(兼容IE,firefox)
在DOM里面操作textarea里面的字符,是比較麻煩的。于是我有這個封裝分享給大家,測試過IE6,8, firefox ,chrome, opera , safari。兼容沒問題。2011-02-02抓取JavaScript動態(tài)加載的內(nèi)容的方法總結(jié)
JavaScript動態(tài)加載的內(nèi)容常見于現(xiàn)代Web應用中,用于增強用戶體驗和減少初始頁面加載時間,然而,這些動態(tài)加載的內(nèi)容對于傳統(tǒng)的網(wǎng)頁抓取工具來說往往是不可見的,本文主要介紹了有JavaScript動態(tài)加載的內(nèi)容如何抓取,需要的朋友可以參考下2024-09-09基于javascript實現(xiàn)tab選項卡切換特效調(diào)試筆記
這篇文章主要介紹了基于javascript實現(xiàn)tab選項卡切換特效調(diào)試筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03JS實現(xiàn)滑動菜單效果代碼(包括Tab,選項卡,橫向等效果)
這篇文章主要介紹了JS實現(xiàn)滑動菜單效果代碼,以實例形式實現(xiàn)了包括Tab,選項卡,橫向等效果,非常簡單實用,需要的朋友可以參考下2015-09-09