小程序自定義tabBar組件封裝
更新時間:2021年11月04日 17:18:30 作者:gyuei
這篇文章主要為大家詳細介紹了小程序自定義tabBar組件封裝,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了小程序自定義tabBar組件封裝的具體代碼,供大家參考,具體內容如下

1、新建組件:在component下新建一個tabBar
2、組件的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="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結構如下:
.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結構如下:
// pages/components/tabBar/index.js
Component({
/**
1. 組件的屬性列表
*/
options: {
multipleSlots: true //在組件定義時的選項中啟用多slot支持
},
properties: {
list: {
type: Array,
value: []
},
selectedColor:{
type: String,
value:''
},
color:{
type: String,
value:''
},
},
/**
2. 組件的初始數據
*/
data: {
tabbarIndex: 0//默認顯示第一個tab元素
},
lifetimes: {
attached() {}
},
/**
3. 組件的方法列表
*/
methods: {
//組件的點擊事件
tabChange(e) {
//獲取到底部欄元素的下標
let index = e.currentTarget.dataset.index;
this.setData({
tabbarIndex:index,
})
//triggerEvent獲取組件的事件
//onMyEvent 頁面?zhèn)鬟^來的點擊事件名稱
this.triggerEvent('onMyEvent',{
tabbarIndex:index,
})
},
}
})
5、組件的index.json結構如下:
{
"component": true,
"usingComponents": {}
}
6、組件在頁面中的使用
7、頁面的json代碼如下:
{
"navigationBarTitleText": "測試",
"usingComponents": {
"mp-tabbar": "../components/tabBar/index"
}
}
8、頁面的wxml代碼如下:
//當選中tab1時頁面顯示的內容
<view wx:if="{{tabbarIndex == 0}}">111111</view>
//當選中tab2時頁面顯示的內容
<view wx:if="{{tabbarIndex == 1}}">222222</view>
//當選中tab3時頁面顯示的內容
<view wx:if="{{tabbarIndex == 2}}">333333</view>
<mp-tabbar list="{{list}}" id='tabComponent' bind:onMyEvent="tabChange"></mp-tabbar>
9、頁面的js代碼如下:
Page({
data: {
tabbarIndex:0,//默認第一個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": "車隊",
"iconPath": "/images/market.png",
"selectedIconPath": "/images/market_selected.png",
}
]
},
/**
* 生命周期函數--監(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)
},
//獲取組件傳遞出來的數據
tabChange:function(e){
let index = e.detail.tabbarIndex;
this.setData({
tabbarIndex:index
})
console.log(e.detail.tabbarIndex)
}
})
最終效果如圖所示:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript中使用webuploader實現上傳視頻功能(demo)
這篇文章主要介紹了webuploader實現上傳視頻功能,通過本文給大家介紹了上傳視頻和上傳圖片的區(qū)別講解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-04-04
JavaScript?數據結構之集合創(chuàng)建(2)
這篇文章主要介紹了JavaScript?數據結構之集合創(chuàng)建,上一篇我們介紹了什么是集合,并且手動實現了一個集合的類,本篇基于上篇內容繼續(xù)深入介紹需要的小伙伴可以參考一下2022-04-04

