微信小程序开发之自定义菜单tabbar
做这个 遇到问题比较多,特此记录以便查看,直接上代码:
一、app.js
控制原有菜单隐藏、启用新菜单、菜单列表,集中在这里控制
hideTabBar这个很关键,解决苹果6S导致的双导航栏:原文https://blog.csdn.net/czp555/article/details/87258301
hideTabBar: function () {
wx.hideTabBar({
fail: function () {
setTimeout(function () {
wx.hideTabBar()
}, 500)
}
});
},
editTabbar: function() {
let tabbar = this.globalData.tabBar;
let currentPages = getCurrentPages();
let _this = currentPages[currentPages.length - 1];
let pagePath = _this.route;
(pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
for (let i in tabbar.list) {
tabbar.list[i].selected = false;
(tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
}
_this.setData({
tabbar: tabbar
});
},
globalData: {
tabBar: {
"color": "#f3f3f3",
"selectedColor": "#f3f3f3",
"borderStyle": "white",
"backgroundColor": "#ff731d",
"list": [{
"pagePath": "/youfan_market/pages/index/index",
"text": "首页",
"iconPath": "/youfan_market/resource/images/tabBar/home.png",
"selectedIconPath": "/youfan_market/resource/images/tabBar/home.png"
},
{
"pagePath": "/youfan_market/pages/category/category",
"text": "分类",
"iconPath": "/youfan_market/resource/images/tabBar/category.png",
"selectedIconPath": "/youfan_market/resource/images/tabBar/category.png"
},
{
"pagePath": "/youfan_market/pages/user/index",
"text": "分享转发",
"iconPath": "/youfan_market/resource/images/tabBar/share.png",
"selectedIconPath": "/youfan_market/resource/images/tabBar/share.png",
isShare: true
},
{
"pagePath": "/youfan_market/pages/cart/cart",
"text": "购物车",
"iconPath": "/youfan_market/resource/images/tabBar/cart.png",
"selectedIconPath": "/youfan_market/resource/images/tabBar/cart.png"
},
{
"pagePath": "/youfan_market/pages/user/index",
"text": "我的",
"iconPath": "/youfan_market/resource/images/tabBar/user.png",
"selectedIconPath": "/youfan_market/resource/images/tabBar/user.png"
}
]
}
},
二、自定义组件
关于图标资源就不发了,需要的自己去iconfont找
在小程序根目录创建组件文件夹 tabbarComponent
tabbar.js
// tabBarComponent/tabBar.js
const app = getApp();
Component({
/**
* 组件的属性列表
*/
properties: {
tabbar: {
type: Object,
value: {
"backgroundColor": "#ffffff",
"color": "#979795",
"selectedColor": "#1c1c1b",
"list": []
}
}
},
/**
* 组件的初始数据
*/
data: {
isIphoneX:false // app.globalData.systemInfo.model == "iPhone X" ? true : false,
}, /**
* 组件的方法列表
*/
methods: {
},
})
tabbar.json
{
"component": true,
"usingComponents": {}
}
tabbar.wxml
<view class="tabbar_box {{isIphoneX?'iphoneX-height':''}}" style="background-color:{{tabbar.backgroundColor}}">
<block wx:for="{{tabbar.list}}" wx:key="{{item.pagePath}}">
<navigator wx:if="{{item.isSpecial}}" class="tabbar_nav" hover-class="none" url="{{item.pagePath}}" style="color:{{tabbar.selectedColor}}" open-type="navigate">
<view class='special-wrapper'><image class="tabbar_icon" src="{{item.iconPath}}"></image></view>
<image class='special-text-wrapper'></image>
{{item.text}}
</navigator>
<button wx:elif='{{item.isShare}}' class="tabbar_nav tabbar_btn" hover-class="none" style="color:{{tabbar.color}};border-radius:90%;" open-type="share">
<image class="tabbar_icon" src="{{item.iconPath}}"></image>
{{item.text}}
</button>
<!-- <navigator wx:else class="tabbar_nav" hover-class="none" url="{{item.pagePath}}" style="color:{{item.selected ? tabbar.selectedColor : tabbar.color}}" open-type="redirect">
<image class="tabbar_icon" src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image>
<text>{{item.text}}</text>
</navigator> -->
<navigator wx:else class="tabbar_nav" hover-class="none" url="{{item.pagePath}}" style="color:{{item.selected ? tabbar.selectedColor : tabbar.color}}" open-type="switchTab">
<image class="tabbar_icon" src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image>
{{item.text}}
</navigator>
</block>
</view>
tabbar.wxss
.tabbar_box {
display: flex;
flex-direction: row;
justify-content: space-around;
position: fixed;
bottom: 0;
left: 0;
z-index: 999999;
width: 100%;
height: 98rpx;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
}
.tabbar_box.iphoneX-height {
padding-bottom: 66rpx;
}
.middle-wrapper {
position: absolute;
right: 310rpx;
bottom: 0;
background-color: #fff;
width: 120rpx;
height: 120rpx;
border-radius: 50%;
border-top: 2rpx solid #f2f2f3;
}
.middle-wrapper.iphoneX-height {
bottom: 66rpx;
}
.tabbar_nav {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 20rpx;
height: 100%;
position: relative;
}
.tabbar_btn {
padding: 0;
border: none;
background: #ff731d;
}
.tabbar_icon {
width: 50rpx;
height: 50rpx;
}
.special-wrapper {
position: absolute;
left: 77rpx;
top: -36rpx;
width: 96rpx;
height: 96rpx;
border-radius: 50%;
border-top: 2rpx solid #f2f2f3;
background-color: #fff;
text-align: center;
box-sizing: border-box;
padding: 6rpx;
}
.special-wrapper .tabbar_icon {
width: 84rpx;
height: 84rpx;
}
.special-text-wrapper {
width: 56rpx;
height: 56rpx;
}
三、页面调用
js
var app = getApp()
Page({
data: {
tabbar: {},
},
onLoad: function(options) {
app.editTabbar(); },
onReady: function () {
app.hideTabBar();
},
onShow: function () {
app.hideTabBar();
},
json
{
"enablePullDownRefresh": false,
"usingComponents": {
"tabbar": "../../tabbarComponent/tabbar"
}
}
wxml 底部添加
<tabbar tabbar="{{tabbar}}"></tabbar>
OK,万事大吉了
最后要说的没源码,没时间去弄
推荐一篇别人的写的:https://www.jianshu.com/p/2cd4a23b504b
微信小程序开发之自定义菜单tabbar的更多相关文章
- 微信小程序底部实现自定义动态Tabbar
多图警告!!! 最近在工作中遇到这样一个需求:微信小程序底部的Tab需要通过判断登录人的角色动态进行改变,想要实现这个功能依靠小程序原生的Tabbar是不可能实现的了,所以研究了一下自定义Tab,这里 ...
- 手摸手教你微信小程序开发之自定义组件
前言 相信大家在开发小程序时会遇到某个功能多次使用的情况,比如弹出框.这个时候大家首先想到的是组件化开发,就是把弹出框封装成一个组件,然后哪里使用哪里就调用,对,看来大家都是有思路的人,但是要怎样实现 ...
- 微信小程序开发之下拉菜单
实现功能:点击维保人员,调出下拉菜单.选择子菜单时,显示右边的图标表示选中,并进行赋值并进行搜索筛选 Wxml: <view class="dtclass" bindtap= ...
- 微信小程序开发学习资料
作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 零基础入门微信小程序开发
注:本文来源于:<零基础入门微信小程序开发> 课程介绍 本达人课是一个系列入门教程,目标是从 0 开始带领读者上手实战,课程以微信小程序的核心概念作为主线,介绍配置文件.页面样式文件.Ja ...
- 总结微信小程序开发中遇到的坑
总结微信小程序开发中遇到的坑,一些坑你得一个一个的跳啊,/(ㄒoㄒ)/~~ 1,页面跳转和参数传递实例 首先说一下我遇到的需求有一个我的消息页面,里面的数据都是后端返回的,返回的数据大致如下,有一个是 ...
- 微信小程序开发中的二三事之网易云信IMSDK DEMO
本文由作者邹永胜授权网易云社区发布. 简介 为了更好的展示我们即时通讯SDK强悍的能力,网易云信IM SDK微信小程序DEMO的开发就提上了日程.用产品的话说就是: 云信 IM 小程序 SDK 的能力 ...
- 《腾讯游戏人生》微信小程序开发总结
为打通游戏人生擂台赛与线下商家的O2O衔接,同时响应时下日臻火热的微信小程序,项目团队决定也开发一款针对性的微信小程序,以此方便商家在我们平台入驻并进行擂台赛事的创建和奖励的核销,进一步推广擂台赛的玩 ...
- 我们的微信小程序开发
基于微信小程序的系统开发准备工作 腾讯推出微信小程序也有一段时间了,在各种行业里面也都掀起一阵阵的热潮,很多APP应用被简化为小程序的功能迅速推出,同时也根据小程序的特性推出各种独具匠心的应用,相对传 ...
随机推荐
- zoj 3601
链接 [https://vjudge.net/contest/293343#problem/B] 题意 就是n男m女.然后给出他们喜欢那些人 再给出q次询问 每次参加party的人 让你找出某个人满足 ...
- Colorful Bricks CodeForces - 1081C ( 组合数学 或 DP )
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in t ...
- pytorch错误:RuntimeError: received 0 items of ancdata解决
版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com RuntimeError: received 0 items of ancdata错误是在datalo ...
- Activiti6-TaskService(学习笔记)重要
任务管理服务: 可以看出来,TaskService操作对象,主要针对于UserTask, 对于业务方来说,最重要的就是用户任务,可以对用户任务进行增删改查的管理.可以对相关流程的控制.也可以设置一些用 ...
- OO_第二单元总结
第二次总结博客(电梯单元) 16071070 陈泽寅 2019.4.20 一:多线程实验初感 这个单元是多线程设计的实践单元,主要让我们运用多线程的原理与思想去完成一个模拟电梯运行的策略.从最开始的单 ...
- ADT打开layout目录的xml报错java.lang.NullPointerException
原因为使用了Android Studio的绿色JRE,必须要安装安装版JDK或者JRE,绿色版JRE放在ADT目录虽然能启动ADT但是不能启动layout目录的xml
- vue stylus 格式化问题
IDE是vscode 安装了.vetur插件 由于stylus可以仅用缩进不用写大括号之类的,所以十分方便, 但有个问题,按alt shift F 格式化时,vetur这个插件会默认添加上正常css的 ...
- EOF输入
EOF是一个计算机术语,为End Of File的缩写,在操作系统中表示资料源无更多的资料可读取.资料源通常称为档案或串流.通常在文本的最后存在此字符表示资料结束.是int类型的宏定义,它扩展为负整数 ...
- “三次握手,四次挥手”你真的懂吗?TCP
“三次握手,四次挥手”你真的懂吗? mp.weixin.qq.com 来源:码农桃花源 解读:“拼多多”被薅的问题出在哪儿?损失将如何买单? 之前有推过一篇不错的干货<TCP之三次握手四次挥手 ...
- vue路由实现多视图的单页应用
多视图的单页应用:在一个页面中实现多个页面不同切换,url也发生相应变化. router-view结合this.$router.push("/pickUp")实现,效果如下: 当点 ...