微信小程序实现自定义底部导航

网友投稿 486 2023-11-11

本文实例为大家分享了微信小程序实现底部导航的具体代码,供大家参考,具体内容如下

微信小程序实现自定义底部导航

建一个tabbar.wxml组件

?
1
2
3
4
5
6
7
8
9
10
11
12
<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中定义

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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; //根据页面地址设置当前页面状态
}
}
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": "/img/mine-2.png",
"selectedColor": "#4877BD",
"clas": "menu-item",
active: false
}
],
"position": "bottom"
},
},

在需要用的导航的页面的wxml

在需要用的导航的页面的js中

?
1
2
3
4
5
const app = getApp();
onShow: function () {
app.editTabBar(); //显示自定义的底部导航
},

为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。

您可能感兴趣的文章:微信小程序自定义底部导航带跳转功能微信小程序实现底部导航微信小程序tabbar底部导航微信小程序tabBar底部导航中文注解api详解详解微信小程序设置底部导航栏目方法微信小程序实战之仿android fragment可滑动底部导航栏(4)微信小程序 底部导航栏目开发资料微信小程序 (三)tabBar底部导航详细介绍

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序WXSS样式文件教程
下一篇:微信小程序学习之wxs使用教程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~