微信小程序本来封装有底部导航栏,但对于想自定义样式和方法的开发者来说,这并不是很好。

参考链接:https://github.com/ljybill/miniprogram-utils/tree/master/custom-tabbar

首先创建一个底部导航栏组件,名为:navBar

<view class='tabbar'>
<view wx:if='{{_auth >= item.auth}}' class='tabbar-item' wx:for='{{tabbarList}}' wx:key='{{item.pagePath}}' bindtap='handleItemTap' data-path='{{item.pagePath}}' data-idx='{{index}}'>
<view class='tabbar-item-icon'>
<image src='{{activeIdx === index ? item.selectedIconPath : item.iconPath}}'></image>
</view>
<view class='tabbar-item-text {{activeIdx === index ? "active" : ""}}'>{{item.text}}</view>
</view>
</view>

导航栏样式,分为5个按钮,中间为圆形凸起

.tabbar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 100rpx;
border-top: 0.5px solid #d5d5d5;
display: flex;
font-size: 0;
background: #fff;
}
.tabbar-item {
flex: 1;
text-align: center;
overflow: hidden;
box-sizing: border-box;
padding: 8rpx 10rpx 0;
color: #333333;
} .tabbar-item:nth-child(3){
position: relative;
bottom: 50rpx;
height:150rpx;
flex: 0.7;
} .tabbar-item:nth-child(3) .tabbar-item-icon{
height: 100rpx;
background: #fff;
border-radius: 50%;
border-top: solid 1px gray;
} .tabbar-item:nth-child(3) .tabbar-item-icon image{
width: 100rpx;
height: 106rpx;
padding-top: 6rpx;
} .tabbar-item:nth-child(3) .tabbar-item-text{
line-height: 0;
font-size: 28rpx;
margin-top: 8px;
color: #808080;
} .tabbar-item-icon {
margin-bottom: 6rpx;
height: 56rpx;
} .tabbar-item-icon image {
width: 56rpx;
height: 56rpx;
} .tabbar-item-text {
font-size: 28rpx;
line-height: 20rpx;
color: #808080;
}
.active {
color: #23ac38;
}

接下来在app.json中配置导航跳转,由于关于项目,路径自己写

"tabBar": {
"color": "white",
"borderStyle": "white",
"backgroundColor": "white",
"list": [
{
"pagePath": "跳转路径",
"text": "首页",
"iconPath": "zh_tcwq/image/home.png",
"selectedIconPath": "zh_tcwq/image/home_selected.png",
"auth": 0
},
{
"pagePath": "跳转路径",
"text": "行业社区",
"iconPath": "zh_tcwq/image/community.png",
"selectedIconPath": "zh_tcwq/image/community_selected.png",
"auth": 0
},
{
"pagePath": "跳转路径",
"text": "发现",
"iconPath": "zh_tcwq/image/xbz_logo.png",
"selectedIconPath": "zh_tcwq/image/close.png",
"auth": 0
},
{
"pagePath": "跳转路径",
"text": "项目对接",
"iconPath": "zh_tcwq/image/docking.png",
"selectedIconPath": "zh_tcwq/image/docking_selected.png",
"auth": 0
},
{
"pagePath": "跳转路径",
"text": "我的",
"iconPath": "zh_tcwq/image/mine.png",
"selectedIconPath": "zh_tcwq/image/mine_selected.png",
"auth": 0
}
]
}

然后在app.js文件中把app.json配置的导航栏隐藏

onLaunch: function() {
// 隐藏原生的tabbar
wx.hideTabBar()
},

由于要自定义导航栏,所以要自己创建一个导航配置文件

在项目目录下创建一个config文件夹,创建一个router.js的文件

module.exports = [
{
"pagePath": "跳转路径",
"text": "首页",
"iconPath": "/zh_tcwq/image/home.png",
"selectedIconPath": "/zh_tcwq/image/home_selected.png",
"auth": 0
},
{
"pagePath": "跳转路径",
"text": "行业社区",
"iconPath": "/zh_tcwq/image/community.png",
"selectedIconPath": "/zh_tcwq/image/community_selected.png",
"auth": 0
},
{
"pagePath": "跳转路径",
"text": "发现",
"iconPath": "/zh_tcwq/image/xbz_logo.png",
"selectedIconPath": "/zh_tcwq/image/close.png",
"auth": 0
},
{
"pagePath": "跳转路径",
"text": "项目对接",
"iconPath": "/zh_tcwq/image/docking.png",
"selectedIconPath": "/zh_tcwq/image/docking_selected.png",
"auth": 0
},
{
"pagePath": "跳转路径",
"text": "我的",
"iconPath": "/zh_tcwq/image/mine.png",
"selectedIconPath": "/zh_tcwq/image/mine_selected.png",
"auth": 0
}
]

接下来在导航栏组件中配置该导航按钮参数

// zh_tcwq/pubcoms/navbar/navbar.js
import tabbarList from "../../../zh_tcwq/config/router.js"
Component({ /**
* 组件的属性列表
*/
properties: {
activeIdx: {
type: Number,
value: 0
},
auth: {
type: Number,
value: 0,
observer: 'onAuthChanged'
}
}, /**
* 组件的初始数据
*/
data: {
tabbarList: tabbarList,
_auth: 0
}, /**
* 组件的方法列表
*/
methods: {
handleItemTap(e) {
const {
idx,
path
} = e.currentTarget.dataset if (idx === this.data.activeIdx) {
this.trigger('refresh')
return
}
wx.switchTab({
url: `/${path}`,
})
},
onAuthChanged(newVal) {
wx.setStorageSync('__com-tabbar-auth', newVal)
this.setData({
_auth: newVal
})
},
trigger(eventName, value = {}, info) {
if (!eventName) {
throw new TypeError('没有自定义事件名')
}
this.triggerEvent(eventName, value)
console.log(`发送 ${eventName} 事件,携带的值为 ${typeof value === 'object' ? JSON.stringify(value) : value} ${info ? ' --- ' + info : ''}`)
}
},
ready() { },
/** 权限显示 */
pageLifetimes: {
show: function () {
console.log('show')
this.setData({
_auth: wx.getStorageSync('__com-tabbar-auth')
})
}
}
})

关于原作者点击跳转后无法动态激活底部样式的问题,我利用了一个小小的机智,通过组件间的参数传递,判断该参数是否和页面传递的参数一致,一致就显示为active状态

例如:在index页面下引用该组件:

先在json中配置组件:

{
"usingComponents": {
"tabbar":"../../../pubcoms/navbar/navbar"
}
}

在wxml文件中引用

<!-- 底部导航栏--start -->

<tabbar activeIdx="{{activeIdx}}"></tabbar>
<!-- 底部导航栏--end -->

利用activeIdx来传入导航栏组件中,在index.js中声明该参数,因为我的index为第一页,所有我赋值的量为0(数组显示的规则是0,1,2,3,4,五个菜单)

每个引用该组件的页面都配置一下activeIdx,这样在每次加载的时候组件会判断两个参数是否一致,如果一致,就添加active样式。

不知道将的够不够清除,如果有问题的同学可以回复提问。

效果图如下,中间是一个圆形凸起的大logo

微信小程序自定义底部导航栏组件+跳转的更多相关文章

  1. 微信小程序-自定义底部导航

    代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  2. 微信小程序 自定义头部导航栏和导航栏背景图片 navigationStyle

    ​ 这两天因为要做一个带背景的小程序头,哭了,小程序导航栏有背景也就算了,还得让导航栏上的背景顺下来,心态小崩.现在可以单独设置一个页面的小程序头了,但是前提是要微信7.0以上的版本,考虑到兼容性问题 ...

  3. 微信小程序自定义头部导航栏

    <!--index.wxml--> <view> <navbar id='index_header' background='{{background}}' pageNa ...

  4. 微信小程序添加底部导航栏

    修改 app.json 文件即可 "tabBar": { "selectedColor": "#1296db", "list&qu ...

  5. 微信小程序~TabBar底部导航切换栏

    底部导航栏这个功能是非常常见的一个功能,基本上一个完成的app,都会存在一个导航栏,那么微信小程序的导航栏该怎么实现呢?经过无数的踩坑,终于实现了,好了,先看看效果图. 对于底部导航栏,小程序上给出的 ...

  6. 微信小程序-自定义菜单导航(实现楼梯效果)

    设计初衷 在开发页面时,往往需要实现,点击页面的导航菜单页面滚动到相应位置,滚动页面实现菜单选项的高亮.在html开发中,我们可以用到a标签锚点实现,jq的动画相结合实现类似效果.在框架中vant U ...

  7. 微信小程序设置底部导航栏目方法

    微信小程序底部想要有一个漂亮的导航栏目,不知道怎么制作,于是百度找到了本篇文章,分享给大家. 好了 小程序的头部标题 设置好了,我们来说说底部导航栏是如何实现的. 我们先来看个效果图 这里,我们添加了 ...

  8. 微信小程序实现navbar导航栏

    一.效果图 二.涉及到组件 1.view组件 2.swiper组件 三.原理 整体来讲是比较简单的,顶部的navbar是使用flex进行布局的:下面的内容区域则是使用到swiper组件,使用方式比较简 ...

  9. 微信小程序自定义顶部导航

    注释:自定义导航需要自备相应图片 一.设置自定义顶部导航 Navigation是小程序的顶部导航组件,当页面配置navigationStyle设置为custom的时候可以使用此组件替代原生导航栏. 1 ...

随机推荐

  1. Java多线程学习——例子:模拟电影院抢座位

    Cinema——List<Integer>数据结构存储电影院座位 public class Cinema{ private List<Integer> seats; //剩余座 ...

  2. 使用使用dockerfile构建webapi镜像然后使用link和bridge两种方式进行桥接

    首先新增一个webapi的项目 项目核心代码 UserContext using Microsoft.EntityFrameworkCore; using System; using System.C ...

  3. TS学习笔记----(一)基础类型

    布尔值: boolean let isDone: boolean = false; 数字: number 和JavaScript一样,TS里的所有数字都是浮点数. 支持十进制和十六进制字面量,TS还支 ...

  4. 解决react项目中跨域和axios封装使用

    最新几天学了一下react,发现了几个问题,估计新入坑的同学们也会遇到,下面我先列出来几点 1.请求跨域问题 2.如何发起请求 3.axios的简单封装 全局安装create-react-app脚手架 ...

  5. for循环、列表的切片、元组

    一.遍历整个列表 使用for语句循环将列表每取出一个变量,然后存储在中间变量中,打印中间变量:循环取出: 1.简单for循环 示例: carssa = ['richan','fengtian','be ...

  6. Bean的构造器注入和setter注入

    链接:https://pan.baidu.com/s/1vixLrr8harzZMwLsIB1Mwg 提取码:ou1n 首先要明白,为什么要注入? IOC容器会在初始化时,创建好所有的bean对象的实 ...

  7. [Python3] 001 "Hello World" 与注释

    目录 1. 致敬 1.1 致敬 "Hello World" 1.2 致敬 Python 之父 Guido van Rossum 2. 注释 2.1 单行注释 2.2 多行注释 3. ...

  8. DateHandler日期处理工具(JSP中使用后台工具类)

    1.DateHandler.java package Utils.dateHandler; import java.text.ParseException; import java.text.Simp ...

  9. [LeetCode] 132. 分割回文串 II

    题目链接 : https://leetcode-cn.com/problems/palindrome-partitioning-ii/ 题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子 ...

  10. 搜索专题: HDU1428漫步校园

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...