微信小程序官方動態(tài)自定義底部tabBar的例子
最近在做小程序的項目,由于用戶需求在進入頁面時進行選則角色,然后再進入小程序,這時看到的底部菜單看到的不一樣,而我們都知道原生的tabBar是不支持這種做法的。在網(wǎng)上搜集了海量資料后發(fā)現(xiàn)無非兩種解決方法:1.是建立模板,可是會有不能忍受的抖動;2.是利用小程序的組件開發(fā);但是項目已經(jīng)進行了一般,不可能再進行大的改變;
怎么辦呢??
無意在一條評論中發(fā)現(xiàn)說微信官方已經(jīng)提供該組件,于是在小程序文檔中尋找很久才在一個犄角旮旯找到:地址鏈接、
先看效果圖:
1.首先看一下官方用法
這是幾個非常重要你需要知道的
2.小程序提供了一個例子
在這個例子中有一個微信官方提供的組件:下載地址
組件的目錄結(jié)構(gòu):
3.就是我們要根據(jù)需求改造
自己改造的目錄結(jié)構(gòu)如下:
1.首先配置app.json
"tabBar": { "custom": true,//這個要有, 前面都提到過,注釋要刪了 "color": "#7A7E83", "selectedColor": "#3cc51f", "borderStyle": "black", "backgroundColor": "#ffffff", "list": [ { "pagePath": "pages/index1/index1", "iconPath": "/image/home.png", "selectedIconPath": "/image/home1.png", "text": "首頁1" }, { "pagePath": "pages/mine1/mine1", "iconPath": "/image/user.png", "selectedIconPath": "/image/user1.png", "text": "我的1" }, { "pagePath": "pages/index2/index2", "iconPath": "/image/home.png", "selectedIconPath": "/image/home1.png", "text": "首頁2" }, { "pagePath": "pages/mine2/mine2", "iconPath": "/image/user.png", "selectedIconPath": "/image/user1.png", "text": "我的2" } ] },//這個 "usingComponents": {}
2.app.js
//app.js App({ onLaunch: function () { }, globalData: { userInfo: null, list:[] //存放tabBar的數(shù)據(jù) } })
3.改造組件
custom-tab-bar/index.js const app =getApp(); Component({ data: { selected: 0, color: "#7A7E83", selectedColor: "#ff6700", list: [] //tabBar的數(shù)據(jù) }, lifetimes: { //組件的生命周期函數(shù) attached() { this.setData({ list: app.globalData.list }) }, }, methods: { switchTab(e) { const data = e.currentTarget.dataset const url = data.path wx.switchTab({url}) this.setData({ selected: data.index }) } } })
4.下面就是用了
在首頁index.wxml定義了兩個按鈕
<button bindtap='tab1'>進入tab1</button> <button bindtap='tab2'>進入tab2</button>
時間:index.js
tab1: function() { app.globalData.list = [{ "pagePath": "/pages/index1/index1", "iconPath": "/image/home.png", "selectedIconPath": "/image/home1.png", "text": "首頁1" }, { "pagePath": "/pages/mine1/mine1", "iconPath": "/image/user.png", "selectedIconPath": "/image/user1.png", "text": "我的1" } ] wx.switchTab({ url: '../index1/index1', }) }, tab2: function() { app.globalData.list = [{ "pagePath": "/pages/index2/index2", "iconPath": "/image/home.png", "selectedIconPath": "/image/home1.png", "text": "首頁2" }, { "pagePath": "/pages/mine2/mine2", "iconPath": "/image/user.png", "selectedIconPath": "/image/user1.png", "text": "我的2" } ] wx.switchTab({ url: '../index2/index2', }) }
5.在每個tabBar中的onshow()方法中添加如下:
//添加選中效果 if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 0 //這個數(shù)是,tabBar從左到右的下標,從0開始 }) }
另外調(diào)整基礎庫版本的地方在
總結(jié)
以上所述是小編給大家介紹的微信小程序官方動態(tài)自定義底部tabBar的例子,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
使用coffeescript編寫node.js項目的方法匯總
Node.js 基于JavaScript編寫應用,JavaScript是我的主要開發(fā)語言。CoffeeScript是編譯為JavaScript的編程語言。CoffeeScript是一個非常高階的語言,將JavaScript、Ruby和Python中我最愛的部分結(jié)合在了一起。小編給大家介紹下使用coffeescript編寫node.js項目的方法2015-08-08javascript下利用數(shù)組緩存正則表達式的實現(xiàn)方法
利用組存大法要提高我們程序的性能,讓我們的正則表達式的創(chuàng)建于執(zhí)行更有效率。2009-12-12使用JavaScript動態(tài)設置樣式實現(xiàn)代碼及演示動畫
使用onmouseover和onmouseout事件實現(xiàn)不同的效果而且是使用js動態(tài)實現(xiàn),本文有利于鞏固你js與css方面的知識,感興趣的你可以了解下哦,希望本文對你有所幫助2013-01-01