微信小程序實現自定義底部導航
更新時間:2020年11月18日 09:20:03 作者:Archer_yy
這篇文章主要為大家詳細介紹了微信小程序實現自定義底部導航,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序實現底部導航的具體代碼,供大家參考,具體內容如下
建一個tabbar.wxml組件
<template name="tabBar"> <view class="tab-bar" style="color: {{tabBar.color}}; background: {{tabBar.backgroundColor}}; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}};"> <block wx:for="{{tabBar.list}}" wx:key="pagePath"> <navigator hover-class="none" url="{{item.pagePath}}" open-type="redirect" class="tabbar_item {{item.clas}}" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : ''}};position:relative;"> <image src="{{item.selectedIconPath}}" wx:if="{{item.active&&item.selectedIconPath}}" class="img"></image> <image src="{{item.iconPath}}" wx:if="{{!item.active&&item.iconPath}}" class="img"></image> <image src="{{item.logo}}" wx:if="{{item.logo}}" style="width:110rpx;height:110rpx;border:10rpx solid #ffffff;border-radius:50%;position:absolute;top:-50%;left:-40%,"></image> <text class="tabbar_text" wx:if="{{item.text}}">{{item.text}}</text> </navigator> </block> </view> </template>
app.js中定義
editTabBar: function () { //使用getCurrentPages可以獲取當前加載中所有的頁面對象的一個數組,數組最后一個就是當前頁面。 var curPageArr = getCurrentPages(); //獲取加載的頁面 var curPage = curPageArr[curPageArr.length - 1]; //獲取當前頁面的對象 var pagePath = curPage.route; //當前頁面url 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; //根據頁面地址設置當前頁面狀態(tài) } } curPage.setData({ tabBar: tabBar }); }, globalData: { tabBar: { "color": "#B0B6B8", "selectedColor": "#4877BD", "backgroundColor": "#ffffff", "borderStyle": "#ccc", "list": [{ "pagePath": "/pages/messagelist/messagelist", "text": "消息", "iconPath": "/img/message-1.png", "selectedIconPath": "/img/message-2.png", "selectedColor": "#4877BD", "clas": "menu-item", active: false }, { "pagePath": "/pages/salefriend/salefriend", "text": "名片", "iconPath": "/img/card-1.png", "selectedIconPath": "/img/card-2.png", "selectedColor": "#4877BD", "clas": "menu-item", active: false }, { "pagePath": "/pages/customnav/customnav", "text": "", // "iconPath": "/img/gifts.png", // "selectedIconPath": "/img/gifts.png", "logo":"/img/logo.png", "selectedColor": "#4877BD", "clas": "menu-item", islogo:true, active: false }, { "pagePath": "/pages/salescircle/salescircle", "text": "發(fā)現", "iconPath": "/img/discover-1.png", "selectedIconPath": "/img/discover-2.png", "selectedColor": "#4877BD", "clas": "menu-item", active: false }, { "pagePath": "/pages/mine/mine", "text": "我的", "iconPath": "/img/mine-1.png", "selectedIconPath": "/img/mine-2.png", "selectedColor": "#4877BD", "clas": "menu-item", active: false } ], "position": "bottom" }, },
在需要用的導航的頁面的wxml
<import src="/components/tabbar.wxml" /> <template is="tabBar" data="{{tabBar}}" />
在需要用的導航的頁面的js中
const app = getApp(); onShow: function () { app.editTabBar(); //顯示自定義的底部導航 },
為大家推薦現在關注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
uniapp使用navigateBack方法返回上級頁面并刷新的簡單示例
最近寫uniapp項目的時候發(fā)現有時候需要更新頁面數據,所以下面這篇文章主要給大家介紹了關于uniapp使用navigateBack方法返回上級頁面并刷新的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-03-03JavaScript Event學習第四章 傳統的事件注冊模型
在這一章我會講解給元素注冊事件的最好的一種辦法,那就是:確保一個特定的事件在特定的HTML元素上發(fā)生并且能運行特定的腳本。2010-02-02JS庫之Particles.js中文開發(fā)手冊及參數詳解
因為自己需要做產品,所以一個好的UI界面也是很重要的,發(fā)現這種散射的原子顆粒特效還不錯。今天腳本之家小編把Particles.js中文開發(fā)手冊及particles.js參數分享給大家,需要的朋友參考下吧2017-09-09