關(guān)于微信小程序自定義tabbar問(wèn)題詳析
1、首先按照官方組件在app.json中定義tabbar
"tabBar": { "custom": true, "backgroundColor": "#FFFFFF", "borderStyle": "white", "list": [ { "pagePath": "pages/index/index", "text": "首頁(yè)", "iconPath": "./images/home.png", "selectedIconPath": "./images/home.png" }, { "pagePath": "pages/me/me", "text": "個(gè)人中心", "iconPath": "./images/me.png", "selectedIconPath": "./images/me.png" } ] }, "usingComponents": {}
2、在項(xiàng)目根目錄創(chuàng)建自定義tabbar組件
劃重點(diǎn):根目錄,請(qǐng)看下圖,不放根目錄會(huì)導(dǎo)致this.getTabBar = null
3、組件內(nèi)容如下:
- custom-tab-bar/index.js
Component({ data: { selected: 0, color: "#7A7E83", selectedColor: "#3cc51f", list: [{ pagePath: "../../pages/index/index", iconPath: "../images/home.png", selectedIconPath: "../images/home.png", text: "首頁(yè)" }, { pagePath: "../../pages/me/me", iconPath: "../images/me.png", selectedIconPath: "../images/me.png", text: "個(gè)人中心" }] }, attached() { }, methods: { switchTab(e) { const data = e.currentTarget.dataset const url = data.path wx.switchTab({url}) this.setData({ selected: data.index }) } } })
- custom-tab-bar/index.json
{ "component": true }
- custom-tab-bar/index.wxml
<cover-view class="tab-bar"> <cover-view class="tab-bar-border"></cover-view> <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab"> <cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image> <cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view> </cover-view> </cover-view>
- custom-tab-bar/index.wxss
.tab-bar { position: fixed; bottom: 0; left: 0; right: 0; height: 48px; background: white; display: flex; padding-bottom: env(safe-area-inset-bottom); } .tab-bar-border { background-color: rgba(0, 0, 0, 0.33); position: absolute; left: 0; top: 0; width: 100%; height: 1px; transform: scaleY(0.5); } .tab-bar-item { flex: 1; text-align: center; display: flex; justify-content: center; align-items: center; flex-direction: column; } .tab-bar-item cover-image { width: 27px; height: 27px; } .tab-bar-item cover-view { font-size: 10px; }
4、在pages下的各個(gè)頁(yè)面組件引入tabbar
以首頁(yè)舉例:
- pages/index.json
{ "usingComponents": { "custom-tab-bar": "../../custom-tab-bar/index" } }
- pages/index.js
Page({ onShow: function () { if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 0 }) } } })
- pages/index.wxml
<view class="container"> <custom-tab-bar></custom-tab-bar> </view>
總結(jié)
到此這篇關(guān)于關(guān)于微信小程序自定義tabbar問(wèn)題的文章就介紹到這了,更多相關(guān)微信小程序自定義tabbar內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決layui中table異步數(shù)據(jù)請(qǐng)求不支持自定義返回?cái)?shù)據(jù)格式的問(wèn)題
今天小編就為大家分享一篇解決layui中table異步數(shù)據(jù)請(qǐng)求不支持自定義返回?cái)?shù)據(jù)格式的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08JavaScript實(shí)現(xiàn)自定義媒體播放器方法介紹
本文主要介紹了JavaScript自定義媒體播放器的實(shí)現(xiàn)過(guò)程與方法,具有一定的參考作用,下面跟著小編一起來(lái)看下吧2017-01-01用JavaScript腳本實(shí)現(xiàn)Web頁(yè)面信息交互
這篇文章主要介紹了用JavaScript腳本實(shí)現(xiàn)Web頁(yè)面信息交互2006-10-10js實(shí)現(xiàn)列表自動(dòng)滾動(dòng)循環(huán)播放
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)列表自動(dòng)滾動(dòng)循環(huán)播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07js數(shù)值計(jì)算時(shí)使用parseInt進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換(jquery)
這篇文章主要介紹了js數(shù)值計(jì)算時(shí)使用parseInt進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換(jquery),需要的朋友可以參考下2014-10-10javascript 靜態(tài)對(duì)象和構(gòu)造函數(shù)的使用和公私問(wèn)題
靜態(tài)對(duì)象和構(gòu)造函數(shù)的使用區(qū)別 平常我們會(huì)經(jīng)常使用JSON形式,或者var obj=function(){}亦或function(){}這么幾種對(duì)象的構(gòu)建辦法,有時(shí)會(huì)認(rèn)為這是等價(jià)的辦法,然而他們還有不同。2010-03-03