微信小程序自定義tabbar組件
本文實(shí)例為大家分享了微信小程序自定義tabbar組件的具體代碼,供大家參考,具體內(nèi)容如下
由于項(xiàng)目需求,必須自己寫(xiě)組件:
第一步:在App.json中配置tabBar,自定也組件也必須配置,"custom": true,list里配置所有的tabbar頁(yè)面。
"tabBar": { "custom": true, "color": "red", "selectedColor": "#3b81d7", "backgroundColor": "#000000", "list": [{ "pagePath": "pages/Role/InsureIndex/index", "text": "首頁(yè)" }, { "pagePath": "pages/Role/MineIndex/index", "text": "首頁(yè)" }, { "pagePath": "pages/index/userInfo/userInfo", "text": "我的" }] },
第二步:在pages的同級(jí)目錄新建組件,文件夾名字:custom-tab-bar,自定義組件文件名為index。組件代碼如下,應(yīng)該都能看懂。
index.js
Component({ properties: {}, data: { indexImg: "../static/images/tabBar/tab_icon_home_nor@2x.png", indexSelectImg: "../static/images/tabBar/tab_icon_home_sel@2x.png", aboutUsImg: "../static/images/tabBar/tab_icon_user_nor@2x.png", aboutUsSelectImg: "../static/images/tabBar/tab_icon_user_sel@2x.png", _tabbat: null, iPhoneX: false, urls: ['/pages/Role/InsureIndex/index', '/pages/index/userInfo/userInfo' ] }, attached() { var self = this //此為業(yè)務(wù)代碼,可不看 wx.getStorage({ key: 'userInfo', success: function (res) { const { userRoleCode } = res.data if (userRoleCode == '50' || userRoleCode == '70') { self.setData({ ["urls[0]"]: '/pages/Role/MineIndex/index' }) } else if (userRoleCode == '30' || userRoleCode == '35' || userRoleCode == '40') { self.setData({ ["urls[0]"]: '/pages/Role/InsureIndex/index' }) } } }) wx.getSystemInfo({ success(res) { console.log(res.model) if (res.model.indexOf('iPhone X') >= 0) { self.setData({ iPhoneX: true }) } } }) }, /** * 組件的方法列表 */ methods: { switchTap: function (e) { var self = this var index = e.currentTarget.dataset.index; var urls = self.data.urls wx.switchTab({ url: urls[index], }) } } })
index.wxml
<div class="_tabbar {{iPhoneX?'_iPhoneX':''}}"> <div class="titem {{_tabbat==0?'tCdk':''}}" data-index="0" bind:tap="switchTap"> <image src="{{_tabbat==0?indexSelectImg:indexImg}}" /> <b>首頁(yè)</b> </div> <div class="titem {{_tabbat==1?'tCdk':''}}" data-index="1" bind:tap="switchTap"> <image src="{{_tabbat==1?aboutUsSelectImg:aboutUsImg}}" /> <b>我的</b> </div> </div>
index.wxss
._tabbar { width: 100%; height: 120rpx; display: flex; align-items: center; background: #fff; font-size: 26rpx; color: #999999; box-shadow: 0px -7rpx 13rpx 0px rgba(193, 185, 204, 0.13); } ._tabbar .titem { text-align: center; width: 50%; } ._tabbar .titem image { display: block; margin: auto; width: 48rpx; height: 48rpx; margin-bottom: 10rpx; } ._tabbar .tCdk { color: #37ADFE; } ._iPhoneX { height: 148rpx; }
index.json
{ "component": true, "usingComponents": {} }
以上為組件代碼,點(diǎn)擊tabbar跳轉(zhuǎn)頁(yè)面時(shí),會(huì)重新加載tabbar組件,導(dǎo)致選中樣式一直是默認(rèn)的,因此需要在用到tabbar的頁(yè)面的js文件中做如下操作:(以 “首頁(yè)” 頁(yè)面為例)
onShow: function () { this.getTabBar().setData({ _tabbat: 0 }) },
以上就已經(jīng)完成了,但是看網(wǎng)上大家說(shuō)會(huì)出現(xiàn)兩個(gè)tabBar,我這邊是沒(méi)出現(xiàn)(一個(gè)自定義,一個(gè)自帶的),如果出現(xiàn)的話,在app.js中的onLaunch函數(shù)中加入 wx.hideTabBar() , 隱藏自帶的tabbar就可以了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js數(shù)字轉(zhuǎn)換為float,取N位小數(shù)
在javascript中不分單精度f(wàn)loat和雙精度double,凡事有小數(shù)的變量都認(rèn)為是float,因此要取小數(shù)后的n位,要用方法toFixed(n)來(lái)得到2014-02-02jQuery ajax(復(fù)習(xí))—Baidu ajax request分離版
你沒(méi)有看錯(cuò)標(biāo)題,本文的確是在講Baidu ajax,不過(guò)是很久很久以前的版本了,我們先分析一段簡(jiǎn)單的ajax代碼,來(lái)自早期的百度七巧板項(xiàng)目通過(guò)這個(gè)來(lái)先復(fù)習(xí)一遍ajax的知識(shí)2013-01-01詳解Js 根據(jù)文件夾目錄獲取Json數(shù)據(jù)輸出demo
這篇文章主要為大家介紹了Js 根據(jù)文件夾目錄獲取Json數(shù)據(jù)輸出示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03使用OpenLayers3 添加地圖鼠標(biāo)右鍵菜單
這篇文章主要介紹了使用OpenLayers3 添加地圖鼠標(biāo)右鍵菜單的相關(guān)資料,需要的朋友可以參考下2015-12-12javascript修改IMG標(biāo)簽的src問(wèn)題
javascript修改IMG標(biāo)簽的SRC,在IE6下面圖片修改正常,但在IE7和Firefox下面卻不刷新,下面有個(gè)解決方法,大家可以參考下2014-03-03