小程序自定義tabBar組件封裝
本文實(shí)例為大家分享了小程序自定義tabBar組件封裝的具體代碼,供大家參考,具體內(nèi)容如下

1、新建組件:在component下新建一個(gè)tabBar
2、組件的index.wxml結(jié)構(gòu)如下:
<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="tabChange">
<cover-image src="{{tabbarIndex === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view style="color: {{tabbarIndex === index ? selectedColor : color}}">{{item.text}}</cover-view>
</cover-view>
</cover-view>
3、組件的index.wxss結(jié)構(gòu)如下:
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 60px;
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: 28px;
height: 28px;
margin-bottom: 2px;
}
.tab-bar-item cover-view {
font-size: 10px;
}
4、組件的index.js結(jié)構(gòu)如下:
// pages/components/tabBar/index.js
Component({
/**
1. 組件的屬性列表
*/
options: {
multipleSlots: true //在組件定義時(shí)的選項(xiàng)中啟用多slot支持
},
properties: {
list: {
type: Array,
value: []
},
selectedColor:{
type: String,
value:''
},
color:{
type: String,
value:''
},
},
/**
2. 組件的初始數(shù)據(jù)
*/
data: {
tabbarIndex: 0//默認(rèn)顯示第一個(gè)tab元素
},
lifetimes: {
attached() {}
},
/**
3. 組件的方法列表
*/
methods: {
//組件的點(diǎn)擊事件
tabChange(e) {
//獲取到底部欄元素的下標(biāo)
let index = e.currentTarget.dataset.index;
this.setData({
tabbarIndex:index,
})
//triggerEvent獲取組件的事件
//onMyEvent 頁面?zhèn)鬟^來的點(diǎn)擊事件名稱
this.triggerEvent('onMyEvent',{
tabbarIndex:index,
})
},
}
})
5、組件的index.json結(jié)構(gòu)如下:
{
"component": true,
"usingComponents": {}
}
6、組件在頁面中的使用
7、頁面的json代碼如下:
{
"navigationBarTitleText": "測試",
"usingComponents": {
"mp-tabbar": "../components/tabBar/index"
}
}
8、頁面的wxml代碼如下:
//當(dāng)選中tab1時(shí)頁面顯示的內(nèi)容
<view wx:if="{{tabbarIndex == 0}}">111111</view>
//當(dāng)選中tab2時(shí)頁面顯示的內(nèi)容
<view wx:if="{{tabbarIndex == 1}}">222222</view>
//當(dāng)選中tab3時(shí)頁面顯示的內(nèi)容
<view wx:if="{{tabbarIndex == 2}}">333333</view>
<mp-tabbar list="{{list}}" id='tabComponent' bind:onMyEvent="tabChange"></mp-tabbar>
9、頁面的js代碼如下:
Page({
data: {
tabbarIndex:0,//默認(rèn)第一個(gè)tab元素
color: "#555555",
selectedColor: "#2ea7e0",
//底部欄
list: [{
"text": "市場",
"iconPath": "/images/bazaar.png",
"selectedIconPath": "/images/bazaar_selected.png",
},
{
"text": "充值",
"iconPath": "/images/recharge.png",
"selectedIconPath": "/images/recharge_selected.png",
}, {
"text": "車隊(duì)",
"iconPath": "/images/market.png",
"selectedIconPath": "/images/market_selected.png",
}
]
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
this.tabComponent = this.selectComponent('#tabComponent');
let selectedColor = this.data.selectedColor;
let color = this.data.color;
this.tabComponent.setData({
selectedColor: selectedColor,
color:color
})
console.log(this.tabComponent.data.tabbarIndex)
},
//獲取組件傳遞出來的數(shù)據(jù)
tabChange:function(e){
let index = e.detail.tabbarIndex;
this.setData({
tabbarIndex:index
})
console.log(e.detail.tabbarIndex)
}
})
最終效果如圖所示:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ES6新數(shù)據(jù)結(jié)構(gòu)Map功能與用法示例
這篇文章主要介紹了ES6新數(shù)據(jù)結(jié)構(gòu)Map功能與用法,結(jié)合實(shí)例形式分析了Map的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-03-03
鼠標(biāo)拖動(dòng)改變DIV等網(wǎng)頁元素的大小的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄髽?biāo)拖動(dòng)改變DIV等網(wǎng)頁元素的大小的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
用js實(shí)現(xiàn)小球的自由移動(dòng)代碼
本篇文章小編為大家介紹,用js實(shí)現(xiàn)小球的自由移動(dòng)代碼。需要的朋友參考下2013-04-04
JavaScript中使用webuploader實(shí)現(xiàn)上傳視頻功能(demo)
這篇文章主要介紹了webuploader實(shí)現(xiàn)上傳視頻功能,通過本文給大家介紹了上傳視頻和上傳圖片的區(qū)別講解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04
JavaScript?數(shù)據(jù)結(jié)構(gòu)之集合創(chuàng)建(2)
這篇文章主要介紹了JavaScript?數(shù)據(jù)結(jié)構(gòu)之集合創(chuàng)建,上一篇我們介紹了什么是集合,并且手動(dòng)實(shí)現(xiàn)了一個(gè)集合的類,本篇基于上篇內(nèi)容繼續(xù)深入介紹需要的小伙伴可以參考一下2022-04-04
利用Three.js實(shí)現(xiàn)3D三棱錐立體特效
Three.js是基于原生WebGL封裝運(yùn)行的三維引擎,在所有WebGL引擎中,Three.js是國內(nèi)文資料最多、使用最廣泛的三維引擎。本文將用Three.js實(shí)現(xiàn)3D三棱錐立體特效,感興趣的可以了解一下2022-06-06
js實(shí)現(xiàn)刷新頁面后回到記錄時(shí)滾動(dòng)條的位置【兩種方案可選】
本文主要介紹了頁面的div中有滾動(dòng)條,js實(shí)現(xiàn)刷新頁面后回到記錄時(shí)滾動(dòng)條的位置的兩種方案,需要的朋友可以看下2016-12-12

