⼩程序实现⾃定义底部导航
本⽂实例为⼤家分享了⼩程序实现底部导航的具体代码,供⼤家参考,具体内容如下
建⼀个tabbar.wxml组件
<template name="tabBar">
<view class="tab-bar" >
<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.selectedC
olor) : ''}};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}}" ></image>
<text class="tabbar_text" wx:if="{{}}">{{}}</text>
</navigator>
</block>
</view>
</template>
app.js中定义
editTabBar: function () {
//使⽤getCurrentPages可以获取当前加载中所有的页⾯对象的⼀个数组,数组最后⼀个就是当前页⾯。
var curPageArr = getCurrentPages(); //获取加载的页⾯
var curPage = curPageArr[curPageArr.length - 1]; //获取当前页⾯的对象
var pagePath = ute; //当前页⾯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; //根据页⾯地址设置当前页⾯状态
}
}
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": "发现",
"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什么意思
"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(); //显⽰⾃定义的底部导航
},
为⼤家推荐现在关注度⽐较⾼的⼩程序教程⼀篇:⼩编为⼤家精⼼整理的,希望喜欢。以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论