關(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)文章
JS實(shí)現(xiàn)可改變列寬的table實(shí)例
本文為大家詳細(xì)介紹下通過(guò)JS實(shí)現(xiàn)可改變列寬的table,具體的思路及代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07
JS實(shí)現(xiàn)保留n位小數(shù)的四舍五入問(wèn)題示例
這篇文章主要介紹了JS實(shí)現(xiàn)保留n位小數(shù)的四舍五入問(wèn)題,結(jié)合完整實(shí)例形式分析了javascript針對(duì)小數(shù)四舍五入操作技巧,需要的朋友可以參考下2016-08-08
ESLint詳解及在WebStorm中的應(yīng)用步驟
ESLint是一種JavaScript代碼檢查工具,開(kāi)發(fā)者可以通過(guò)自定義規(guī)則進(jìn)行代碼風(fēng)格和質(zhì)量的控制,使用ESLint的過(guò)程包括安裝、初始化配置、配置規(guī)則、運(yùn)行ESLint檢查代碼、與編輯器集成,以及與構(gòu)建工具集成等,需要的朋友可以參考下2024-09-09
原生JS實(shí)現(xiàn)的多個(gè)彩色小球跟隨鼠標(biāo)移動(dòng)動(dòng)畫(huà)效果示例
這篇文章主要介紹了原生JS實(shí)現(xiàn)的多個(gè)彩色小球跟隨鼠標(biāo)移動(dòng)動(dòng)畫(huà)效果,涉及javascript事件響應(yīng)、頁(yè)面元素屬性動(dòng)態(tài)修改及隨機(jī)數(shù)應(yīng)用等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
解決js相同的正則多次調(diào)用test()返回的值卻不同的問(wèn)題
今天小編就為大家分享一篇解決js相同的正則多次調(diào)用test()返回的值卻不同的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
JavaScript+Canvas實(shí)現(xiàn)帶跳動(dòng)效果的粒子動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)JavaScript和Canvas實(shí)現(xiàn)帶跳動(dòng)效果的粒子動(dòng)畫(huà),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-03-03

