小程序自定義tabBar組件封裝
本文實例為大家分享了小程序自定義tabBar組件封裝的具體代碼,供大家參考,具體內(nèi)容如下
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. 組件的初始數(shù)據(jù) */ 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時頁面顯示的內(nèi)容 <view wx:if="{{tabbarIndex == 0}}">111111</view> //當選中tab2時頁面顯示的內(nèi)容 <view wx:if="{{tabbarIndex == 1}}">222222</view> //當選中tab3時頁面顯示的內(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,//默認第一個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", } ] }, /** * 生命周期函數(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
IE8利用自帶的setCapture和releaseCapture解決iframe的拖拽事件方法
最近有個需求須要實現(xiàn)左右拖拽功能,頁面右邊是個iframe頁面,在chrome測試通過之后,發(fā)現(xiàn)在ie8上面效果不是很理想,查閱相關資料找到可以使用ie自帶的setCapture和releaseCapture來解決,需要的朋友可以參考下2016-10-10layui+ssm實現(xiàn)數(shù)據(jù)表格雙擊編輯更新數(shù)據(jù)功能
在使用layui加載后端數(shù)據(jù)請求時,對數(shù)據(jù)選項框進行雙擊即可實現(xiàn)數(shù)據(jù)的輸入編輯更改,本文結合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-12-12Javascript 獲取字符串字節(jié)數(shù)的多種方法
Javascript 字符串字節(jié)數(shù)獲取功能多種方法2009-06-06JS簡單實現(xiàn)DIV相對于瀏覽器固定位置不變的方法
這篇文章主要介紹了JS簡單實現(xiàn)DIV相對于瀏覽器固定位置不變的方法,涉及javascript針對頁面位置的運算與動態(tài)變換技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06JS前端開發(fā)之exec()和match()的對比使用
match()方法可在字符串內(nèi)檢索指定的值,或找到一個或多個正則表達式的匹配,下面這篇文章主要給大家介紹了關于JS前端開發(fā)之exec()和match()的對比使用的相關資料,需要的朋友可以參考下2022-09-09JavaScript+html5 canvas繪制漸變區(qū)域完整實例
這篇文章主要介紹了JavaScript+html5 canvas繪制漸變區(qū)域的方法,結合完整實例形式分析了canvas顏色調用與圖形繪制的相關技巧,需要的朋友可以參考下2016-01-01