小程序自定義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": "測(cè)試", "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": "市場(chǎng)", "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) } })
最終效果如圖所示:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript瀑布流布局實(shí)現(xiàn)方法詳解
這篇文章主要介紹了javascript瀑布流布局實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了JavaScript實(shí)現(xiàn)瀑布流布局的樣式與具體功能代碼,需要的朋友可以參考下2016-02-02IE8利用自帶的setCapture和releaseCapture解決iframe的拖拽事件方法
最近有個(gè)需求須要實(shí)現(xiàn)左右拖拽功能,頁面右邊是個(gè)iframe頁面,在chrome測(cè)試通過之后,發(fā)現(xiàn)在ie8上面效果不是很理想,查閱相關(guān)資料找到可以使用ie自帶的setCapture和releaseCapture來解決,需要的朋友可以參考下2016-10-10layui+ssm實(shí)現(xiàn)數(shù)據(jù)表格雙擊編輯更新數(shù)據(jù)功能
在使用layui加載后端數(shù)據(jù)請(qǐng)求時(shí),對(duì)數(shù)據(jù)選項(xiàng)框進(jìn)行雙擊即可實(shí)現(xiàn)數(shù)據(jù)的輸入編輯更改,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12Javascript 獲取字符串字節(jié)數(shù)的多種方法
Javascript 字符串字節(jié)數(shù)獲取功能多種方法2009-06-06JS簡(jiǎn)單實(shí)現(xiàn)DIV相對(duì)于瀏覽器固定位置不變的方法
這篇文章主要介紹了JS簡(jiǎn)單實(shí)現(xiàn)DIV相對(duì)于瀏覽器固定位置不變的方法,涉及javascript針對(duì)頁面位置的運(yùn)算與動(dòng)態(tài)變換技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06JS前端開發(fā)之exec()和match()的對(duì)比使用
match()方法可在字符串內(nèi)檢索指定的值,或找到一個(gè)或多個(gè)正則表達(dá)式的匹配,下面這篇文章主要給大家介紹了關(guān)于JS前端開發(fā)之exec()和match()的對(duì)比使用的相關(guān)資料,需要的朋友可以參考下2022-09-09JavaScript+html5 canvas繪制漸變區(qū)域完整實(shí)例
這篇文章主要介紹了JavaScript+html5 canvas繪制漸變區(qū)域的方法,結(jié)合完整實(shí)例形式分析了canvas顏色調(diào)用與圖形繪制的相關(guān)技巧,需要的朋友可以參考下2016-01-01