底部導航欄這個功能是非常常見的一個功能,基本上一個完成的app,都會存在一個導航欄,那么微信小程序的導航欄該怎么實現呢?經過無數的踩坑,終於實現了,好了,先看看效果圖。
對於底部導航欄,小程序上給出的文檔要求里面的item最少2個,最多五個。
好了,先看看代碼:
在項目中找到這個文件
1 { 2 "pages":[ 3 "pages/index/index", 4 "pages/logs/logs", 5 "pages/mine/mine" 6 7 ], 8 "window":{ 9 "backgroundTextStyle":"light", 10 "navigationBarBackgroundColor": "#fff", 11 "navigationBarTitleText": "首頁", 12 "navigationBarTextStyle":"black" 13 }, 14 15 "tabBar": { 16 "color": "#a9b7b7", 17 "selectedColor": "#11cd6e", 18 "borderStyle": "black" , 19 "list": [{ 20 "selectedIconPath": "images/icon_consult_press.png", 21 "iconPath": "images/icon_consult.png", 22 "pagePath": "pages/index/index", 23 "text": "首頁" 24 }, { 25 "selectedIconPath": "images/icon_invest_press.png", 26 "iconPath": "images/icon_invest.png", 27 "pagePath": "pages/logs/logs", 28 "text": "一元投" 29 },{ 30 "selectedIconPath": "images/icon_mine_press.png", 31 "iconPath": "images/icon_mine.png", 32 "pagePath": "pages/mine/mine", 33 "text": "我的" 34 } 35 ] 36 } 37 }
這里我先解釋一下這些屬性是什么意思:
tabBar 指底部的 導航配置屬性
color 未選擇時 底部導航文字的顏色
selectedColor 選擇時 底部導航文字的顏色
borderStyle 底部導航邊框的樣色(注意 這里如果沒有寫入樣式 會導致 導航框上邊框會出現默認的淺灰色線條)
list 導航配置數組
selectedIconPath 選中時 圖標路徑
iconPath 未選擇時 圖標路徑
pagePath 頁面訪問地址
text 導航圖標下方文字
這里需要注意一些小問題:
1、每個頁面的json文件都不能去掉navigationBarTitleText這個屬性。否則會報錯
2、
"pages":[ 3 "pages/index/index", 4 "pages/logs/logs", 5 "pages/mine/mine" 6 7 ],
這個頁面的注冊一定要注意,第一個一定是要是最先顯示的,否則會出現底部導航看不到。