微信小程序tabBar模板用法實例分析【附demo源碼下載】
本文實例講述了微信小程序tabBar模板用法。分享給大家供大家參考,具體如下:
眾所周知,微信小程序的tabBar都是新開頁面的,而微信文檔上又表明了最多只能打開5層頁面。這樣就很容易導致出問題啦,假如我的tabBar有5個呢?下面是微信原話:
一個應用同時只能打開5個頁面,當已經(jīng)打開了5個頁面之后,wx.navigateTo不能正常打開新頁面。請避免多層級的交互方式,或者使用wx.redirectTo
因此這幾天想著根據(jù)微信tabBar數(shù)組來自定義模板供頁面調(diào)用。不過我在list里面每個對象都增加了一個selectedColor和active屬性,方便對每個tabBar當前頁做樣式,如果不傳直接使用設(shè)置的selectedColor。因此這串數(shù)據(jù)只能設(shè)定在各個頁面下,不能設(shè)定在公用的app.js配置文件下,稍微有點代碼冗余,下次研究下怎么直接配置到app.js完善下。
只要新建一個tarBar.wxml模板頁,然后引用模板的頁面?zhèn)魅霐?shù)據(jù)即可,代碼如下:
<template name="tabBar">
<view class="flex-h flex-hsb tab-bar" style="color: {{tabBar.color}}; background: {{tarBar.backgroundColor}}; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}}; {{tabBar.borderStyle? (tabBar.position=='top'? 'border-bottom: solid 1px '+tabBar.borderStyle + ';' : 'border-top: solid 1px '+tabBar.borderStyle + ';') : ''}}">
<block wx:for="{{tabBar.list}}" wx:key="pagePath">
<navigator url="{{item.pagePath}}" open-type="redirect" class="menu-item" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : ''}}">
<image src="{{item.selectedIconPath}}" wx:if="{{item.active}}"></image>
<image src="{{item.iconPath}}" wx:if="{{!item.active}}"></image>
<text>{{item.text}}</text>
</navigator>
</block>
</view>
</template>
接下來進行測試,首先是index.js的配置對象:
//配置tabBar
tabBar: {
"color": "#9E9E9E",
"selectedColor": "#f00",
"backgroundColor": "#fff",
"borderStyle": "#ccc",
"list": [
{
"pagePath": "/pages/index/index",
"text": "主頁",
"iconPath": "../../img/tabBar_home.png",
"selectedIconPath": "../../img/tabBar_home_cur.png",
//"selectedColor": "#4EDF80",
active: true
},
{
"pagePath": "/pages/village/city/city",
"text": "目的地",
"iconPath": "../../img/tabBar_village.png",
"selectedIconPath": "../../img/tabBar_village_cur.png",
"selectedColor": "#4EDF80",
active: false
},
{
"pagePath": "/pages/mine/mine",
"text": "我的",
"iconPath": "../../img/tabBar_mine.png",
"selectedIconPath": "../../img/tabBar_mine_cur.png",
"selectedColor": "#4EDF80",
active: false
}
],
"position": "bottom"
}
index.wxml引入模板:
<import src="../../template/tabBar.wxml" />
<template is="tabBar" data="{{tabBar: tabBar}}" />
接下來是mine.js文件引入配置對象:
//配置tabBar
tabBar: {
"color": "#9E9E9E",
"selectedColor": "#f00",
"backgroundColor": "#fff",
"borderStyle": "#ccc",
"list": [
{
"pagePath": "/pages/index/index",
"text": "主頁",
"iconPath": "../../img/tabBar_home.png",
"selectedIconPath": "../../img/tabBar_home_cur.png",
//"selectedColor": "#4EDF80",
active: false
},
{
"pagePath": "/pages/village/city/city",
"text": "目的地",
"iconPath": "../../../img/tabBar_village.png",
"selectedIconPath": "../../../img/tabBar_village_cur.png",
"selectedColor": "#4EDF80",
active: false
},
{
"pagePath": "/pages/mine/mine",
"text": "我的",
"iconPath": "../../img/tabBar_mine.png",
"selectedIconPath": "../../img/tabBar_mine_cur.png",
"selectedColor": "#4EDF80",
active: true
}
],
"position": "bottom"
}
mine.wxml引入模板:
<import src="../../template/tabBar.wxml" />
<template is="tabBar" data="{{tabBar: tabBar}}" />
最后演示如下:

方案二,我把配置數(shù)據(jù)統(tǒng)一放在app.js文件,通過點擊跳轉(zhuǎn)頁面后在把數(shù)據(jù)添加到當前頁面實例上,具體做法如下:
1、app.js文件配置:
//app.js
var net = require('common/net');
var a_l, a_d = {}, a_cbSucc, a_cbSuccFail, a_cbFail, a_cbCom, a_h, a_m;
App({
onLaunch: function () {
var that = this;
},
//修改tabBar的active值
editTabBar: function () {
var _curPageArr = getCurrentPages();
var _curPage = _curPageArr[_curPageArr.length - 1];<span style="font-family: Arial, Helvetica, sans-serif;">//相當于Page({})里面的this對象</span>
var _pagePath=_curPage.__route__;
if(_pagePath.indexOf('/') != 0){
_pagePath='/'+_pagePath;
}
var tabBar=this.globalData.tabBar;
for(var i=0; i<tabBar.list.length; i++){
tabBar.list[i].active=false;
if(tabBar.list[i].pagePath==_pagePath){
tabBar.list[i].active=true;//根據(jù)頁面地址設(shè)置當前頁面狀態(tài)
}
}
_curPage.setData({
tabBar: tabBar
});
},
globalData: {
userInfo: null,
//配置tabBar
tabBar: {
"color": "#9E9E9E",
"selectedColor": "#f00",
"backgroundColor": "#fff",
"borderStyle": "#ccc",
"list": [
{
"pagePath": "/pages/index/index",
"text": "主頁",
"iconPath": "/pages/templateImg/tabBar_home.png",
"selectedIconPath": "/pages/templateImg/tabBar_home_cur.png",
"selectedColor": "#4EDF80",
active: false
},
{
"pagePath": "/pages/village/city/city",
"text": "目的地",
"iconPath": "/pages/templateImg/tabBar_village.png",
"selectedIconPath": "/pages/templateImg/tabBar_village_cur.png",
"selectedColor": "#4EDF80",
active: false
},
{
"pagePath": "/pages/mine/mine",
"text": "我的",
"iconPath": "/pages/templateImg/tabBar_mine.png",
"selectedIconPath": "/pages/templateImg/tabBar_mine_cur.png",
"selectedColor": "#4EDF80",
active: false
}
],
"position": "bottom"
}
}
})
2、index.js+mine.js+city.js頁面使用:
var App=getApp();
Page({
data:{
detail: {},
},
onLoad:function(options){
App.editTabBar();//添加tabBar數(shù)據(jù)
var that=this;
}
})
最終演示和上圖一致!
附:完整demo代碼點擊此處本站下載。
希望本文所述對大家微信小程序開發(fā)有所幫助。
- 微信小程序tabbar底部導航
- 微信小程序開發(fā)之tabbar圖標和顏色的實現(xiàn)
- 微信小程序開發(fā)之自定義tabBar的實現(xiàn)
- 微信小程序踩坑記錄之解決tabBar.list[3].selectedIconPath大小超過40kb
- 微信小程序tabBar用法實例詳解
- 微信小程序tabBar底部導航中文注解api詳解
- 微信小程序 新建登錄頁并實現(xiàn)tabBar隱藏
- 微信小程序tabbar不顯示解決辦法
- 微信小程序開發(fā)之選項卡(窗口底部TabBar)頁面切換
- 微信小程序開發(fā)之Tabbar實例詳解
- 微信小程序開發(fā)之實現(xiàn)選項卡(窗口頂部TabBar)頁面切換
- 微信小程序 (三)tabBar底部導航詳細介紹
- 微信小程序自定義tabBar組件開發(fā)詳解
相關(guān)文章
ECMAScript 6即將帶給我們新的數(shù)組操作方法前瞻
這篇文章主要介紹了ECMAScript 6即將帶給我們新的數(shù)組操作方法前瞻,需要的朋友可以參考下2015-01-01
JavaScript eval() 函數(shù)介紹及應用示例
eval(String) 函數(shù)可計算某個字符串,并執(zhí)行其中的的 JavaScript 代碼,該方法只接受原始字符串作為參數(shù)2014-07-07
JavaScript+CSS實現(xiàn)仿天貓側(cè)邊網(wǎng)頁菜單效果
這篇文章主要介紹了JavaScript+CSS實現(xiàn)仿天貓側(cè)邊網(wǎng)頁菜單效果,涉及javascript鼠標事件及頁面元素動態(tài)操作的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
JS利用window.print()實現(xiàn)網(wǎng)頁打印功能
print作為瀏覽已經(jīng)比較成熟的技術(shù)可以經(jīng)常被用來打印頁面的部分內(nèi)容。本文將在JS中調(diào)用window.print()方法實現(xiàn)網(wǎng)頁打印功能,感興趣的可以跟隨小編一起學習一下2022-04-04
Knockout結(jié)合Bootstrap創(chuàng)建動態(tài)UI實現(xiàn)產(chǎn)品列表管理
這篇文章主要為大家詳細介紹了Knockout結(jié)合Bootstrap創(chuàng)建動態(tài)UI實現(xiàn)產(chǎn)品列表管理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
javascript小數(shù)計算出現(xiàn)近似值的解決辦法
在javascript里面,小數(shù)只能進行相似計算,例如:5.06+1.30,你得到的結(jié)果會是6.359999999999999,但有的小數(shù)計算又是正確的,如果計算出現(xiàn)了近似值,你可以用如下的方法計算。2010-02-02

