關于微信小程序自定義tabbar問題詳析
更新時間:2022年04月02日 11:41:03 作者:貓尾巴
微信小程序是一種全新的連接用戶與服務的方式,它可以在微信內(nèi)被便捷地獲取和傳播,同時具有出色的使用體驗,下面這篇文章主要給大家介紹了關于微信小程序自定義tabbar問題的相關資料,需要的朋友可以參考下
1、首先按照官方組件在app.json中定義tabbar
"tabBar": {
"custom": true,
"backgroundColor": "#FFFFFF",
"borderStyle": "white",
"list": [
{
"pagePath": "pages/index/index",
"text": "首頁",
"iconPath": "./images/home.png",
"selectedIconPath": "./images/home.png"
},
{
"pagePath": "pages/me/me",
"text": "個人中心",
"iconPath": "./images/me.png",
"selectedIconPath": "./images/me.png"
}
]
},
"usingComponents": {}2、在項目根目錄創(chuàng)建自定義tabbar組件
劃重點:根目錄,請看下圖,不放根目錄會導致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: "首頁"
}, {
pagePath: "../../pages/me/me",
iconPath: "../images/me.png",
selectedIconPath: "../images/me.png",
text: "個人中心"
}]
},
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下的各個頁面組件引入tabbar
以首頁舉例:
- 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>
總結
到此這篇關于關于微信小程序自定義tabbar問題的文章就介紹到這了,更多相關微信小程序自定義tabbar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JS實現(xiàn)保留n位小數(shù)的四舍五入問題示例
這篇文章主要介紹了JS實現(xiàn)保留n位小數(shù)的四舍五入問題,結合完整實例形式分析了javascript針對小數(shù)四舍五入操作技巧,需要的朋友可以參考下2016-08-08
原生JS實現(xiàn)的多個彩色小球跟隨鼠標移動動畫效果示例
這篇文章主要介紹了原生JS實現(xiàn)的多個彩色小球跟隨鼠標移動動畫效果,涉及javascript事件響應、頁面元素屬性動態(tài)修改及隨機數(shù)應用等相關操作技巧,需要的朋友可以參考下2018-02-02
JavaScript+Canvas實現(xiàn)帶跳動效果的粒子動畫
這篇文章主要為大家詳細介紹了如何通過JavaScript和Canvas實現(xiàn)帶跳動效果的粒子動畫,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2023-03-03

