微信小程序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)文章
layui問題之自動滾動二級iframe頁面到指定位置的方法
今天小編就為大家分享一篇layui問題之自動滾動二級iframe頁面到指定位置的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09Whatever:hover 無需javascript讓IE支持豐富偽類
絕大部分現(xiàn)代瀏覽器支持 css 中的 :hover 偽類選擇器,可以用于所有 html 元素。2010-06-06JavaScript進制數(shù)之間的互相轉(zhuǎn)換
這篇文章主要介紹了JavaScript進制數(shù)之間的互相轉(zhuǎn)換,進制轉(zhuǎn)換是人們利用符號來計數(shù)的方法,下文基于JavaScript實現(xiàn)進制數(shù)之間的轉(zhuǎn)換,有一定的知識性參考價值,需要的小伙伴可以參考一下2022-05-05