微信小程序自定义tabbar实现突出样式详解流程
作者:code_充电站 发布时间:2024-06-14 05:09:52
前言
昨天主管突然给我说微信小程序默认的 tabBar
不美观,让我改成中间突出的那种样式。纵然我心里面有千般不情愿,但还是接下了这个任务。查了一下文档 自定义 tabBar 发现有这个方法,有思路了就赶紧搞起来,以下是我的开发经验分享。
一、自定义tabbar栏配置
在 app.json 文件中的 tabBar 中指定 custom 字段为 true(意思是允许使用自定义 tabBar);
在 app.json 中全局开启使用组件,或者在所有涉及的 tab 页 json 中申明usingComponents 项;
在 app.json 中添加作为 tabBar 栏的页面;
示例代码
"tabBar": {
"custom": true,
"color": "#afafaf",
"selectedColor": "#0099f6",
"backgroundColor": "#F7F8F8",
"list": [
{
"pagePath": "pages/index/index",
"text": "首页"
},
{
"pagePath": "pages/goodIndexCopy/goodIndexCopy",
"text": "易购商城"
},
{
"pagePath": "pages/release/release",
"text": "发布"
},
{
"pagePath": "pages/nearby/nearby",
"text": "本地"
},
{
"pagePath": "pages/mine/mine",
"text": "我的"
}
]
},
"usingComponents": {},
pagePath
是自己添加的页面,text
是tabBar上展示的文字。
二、添加自定义tabbar栏组件
在根目录下创建 custom-tab-bar
文件夹,并在该文件夹下新建 Component
,或者新建 Page
,但是这种创建方式需要自己改动一些代码,在这里我们选用新建 Component
的方式。
添加组件代码
1、完善 wxml
文件代码,tabBar
栏需要展示的页面是一个固定的数组,可以使用 wx:for
循环展示,在这里用到 selected
这个字段,这个字段的作用是帮助展示 tabBar
选中和未选中的样式。
<!--custom-tab-bar/index.wxml-->
<view class="tab-bar">
<view wx:for="{{list}}" wx:key="index" class="tab-bar-item {{item.bulge?'bulge':''}}" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
<view wx:if="item.bulge" class="tab-bar-bulge"></view>
<image class="image" src="{{selected == index ? item.selectedIconPath : item.iconPath}}"></image>
<!-- <view wx:if="{{item.text}}" style="color: {{selected === index ? selectedColor : color}}" class="tab-bar-view">{{item.text}}</view> -->
<view class="tab-bar-view" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
</view>
</view>
2、完善 js
文件代码,list
数组就是在 tabBar
栏展示的页面信息,switchTab
方法作用可以出看来是负责跳转页面。其它的字段相信各位都知道,这里就不再描述。
/**
* 组件的初始数据
*/
data: {
selected: 0,
color: "#afafaf",
selectedColor: "#0099f6",
backgroundColor: "#F7F8F8",
list: [
{
pagePath: "/pages/index/index",
iconPath: "/images/icon/wtc/icon_zhuye2.png",
selectedIconPath: "/images/icon/wtc/icon_zhuye2_d.png",
text: "首页",
},
{
pagePath: "/pages/goodIndexCopy/goodIndexCopy",
iconPath: "/images/icon/wtc/icon_pintuan2.png",
selectedIconPath: "/images/icon/wtc/icon_pintuan2_d.png",
text: "易购商城"
},
{
pagePath: "/pages/release/release",
bulge:true,
iconPath: "/images/add.png",
selectedIconPath: "/images/add-active.png",
text: "发布"
},
{
pagePath: "/pages/nearby/nearby",
iconPath: "/images/icon/wtc/icon_pintuan3.png",
selectedIconPath: "/images/icon/wtc/icon_pintuan3_d.png",
text: "本地",
},
{
pagePath: "/pages/mine/mine",
iconPath: "/images/icon/wtc/icon_wode3.png",
selectedIconPath: "/images/icon/wtc/icon_wode3_d.png",
text: "我的"
},
]
},
/**
* 组件的方法列表
*/
methods: {
switchTab(e) {
// console.log(e);
const data = e.currentTarget.dataset;
const url = data.path;
wx.switchTab({url})
}
}
3、完善 wxss
文件代码。
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 50px;
background: #FFF;
display: flex;
line-height: 1.2;
padding-bottom: env(safe-area-inset-bottom);
border-top: 1px solid #e6e6e6;
}
.tab-bar-item {
flex: 1;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.tab-bar-item .image {
width: 26px;
height: 26px;
}
.bulge {
background-color: #FFF;
}
.bulge .tab-bar-bulge{
position: absolute;
z-index: -1;
width: 64px;
height: 64px;
top: -24px;
border-radius: 50%;
border-top: 1px solid #e6e6e6;
background-color: #FFF;
}
.bulge .image{
position: absolute;
width: 50px;
height: 50px;
top: -20px;
}
.bulge .tab-bar-view{
position: relative;
bottom: -16px;
margin-top: 4px;
}
.tab-bar-item .tab-bar-view {
font-size: 12px;
margin-top: 4px;
}
创建全局字段
做完以上工作之后,我们可以就可以看一下效果了,是不是就以为这样就可以了呢?但是事与愿违,会发现这里存在 bug
,在我们点击 tabBar
栏进行跳转的时候会发现页面跳转过去了,但是对应页面的 tabBar
没有改变颜色。为了解决这个 bug
,需要添加全局字段。在 app.js 文件中创建该字段。
globalData: {
selected: 0
},
在组件中保存重要字段
全局字段创建完成之后,我们需要在组件 js 文件中使用该字段,在 ready
函数中保存这个字段,在点击 tabBar
栏时,把相应的index
赋值给这个全局字段。
// 引入全局函数
const app = getApp()
Component({
/**
* 组件的初始数据
*/
data: {
selected: 0,
color: "#afafaf",
selectedColor: "#0099f6",
backgroundColor: "#F7F8F8",
list: [
{
pagePath: "/pages/index/index",
iconPath: "/images/icon/wtc/icon_zhuye2.png",
selectedIconPath: "/images/icon/wtc/icon_zhuye2_d.png",
text: "首页",
},
{
pagePath: "/pages/goodIndexCopy/goodIndexCopy",
iconPath: "/images/icon/wtc/icon_pintuan2.png",
selectedIconPath: "/images/icon/wtc/icon_pintuan2_d.png",
text: "易购商城"
},
{
pagePath: "/pages/release/release",
bulge:true,
iconPath: "/images/add.png",
selectedIconPath: "/images/add-active.png",
text: "发布"
},
{
pagePath: "/pages/nearby/nearby",
iconPath: "/images/icon/wtc/icon_pintuan3.png",
selectedIconPath: "/images/icon/wtc/icon_pintuan3_d.png",
text: "本地",
},
{
pagePath: "/pages/mine/mine",
iconPath: "/images/icon/wtc/icon_wode3.png",
selectedIconPath: "/images/icon/wtc/icon_wode3_d.png",
text: "我的"
},
]
},
ready: function() {
this.setData({
selected: app.globalData.selected
})
},
/**
* 组件的方法列表
*/
methods: {
switchTab(e) {
// console.log(e);
const data = e.currentTarget.dataset;
const url = data.path;
app.globalData.selected = data.index;
wx.switchTab({url})
}
}
})
添加完成后,可以测试一下效果,发现刚才的 bug
已经解决。perfect!!
三、效果展示
来源:https://blog.csdn.net/qq_44880095/article/details/128631684


猜你喜欢
- 当大家发现数据库查询性能很慢的时候,大家都会想到加索引来优化数据库查询性能,但是面对一个复杂的SQL语句,找到一个优化的索引组合对人脑来讲,
- python可以使用xlrd读excel,使用xlwt写excel,但是如果要把数据写入已存在的excel,需要另外一个库xlutils配合
- 1.简介torch.autograd.Variable是Autograd的核心类,它封装了Tensor,并整合了反向传播的相关实现Varia
- 本文实例讲述了python生成器/yield协程/gevent写简单的图片下载器功能。分享给大家供大家参考,具体如下:1、生成器:'
- 非常简单的函数,但是官网的介绍令人(令我)迷惑,所以稍加解释。 mask_select会将满足mask(掩码、遮罩等等,随便翻译)的指示,将
- 判断某一个对象里面是否存在某个属性,常见错误场景排查,但是你真的知道该如何使用嘛。关于这个问题,大家第一眼看到脑海中肯定会有多种方案去实现,
- 从string-db下载蛋白质相互作用的信息,在处理时发现蛋白A与B互作被记录了两次比如下边的例子(即AB、BA)df.drop_dupli
- 推导式是Python中很强大的、很受欢迎的特性,具有语言简洁,速度快等优点。推导式包括:1.列表推导式2.字典推导式3.集合推导式嵌套列表推
- 前言HTML 5如同一场革命,正在Web2.0后时代轰轰烈烈的进行着。HTML 5是什么,无须我在这里赘述了。对于HTML 5的革新,按我的
- 安装前的准备1.python的安装和配置在Window下:在开始菜单中找到运行输入cmd或直接搜索cmd点击进入,输入python,如果出现
- 问题1:如何获取caller的(文件名,行号,函数名)?当新增一条log记录时,最终将调用Logger类的_log方法,这个方法首先会创建一
- 什么是转义字符转义字符是一个计算机专业词汇。在计算机当中,我们可以写出123 ,也可以写出字母abcd,但有些字符我们无法手动书写,比如我们
- 前言这篇文章主要记录一下平时自己实践得到的, 博客中学习的以及在一些项目源码中看到的 javascript 技巧。有些东西可以说是奇淫技巧,
- 本文转自微信公众号:算法与编程之美一、引言在具备一定的Python编程基础以后,我们可以结合for循环进行多角星的编写,只要简单的几次循环,
- 前言: 在各类技术岗位面试中,似乎 MySQL 相关问题经常被问到。无论你面试开发岗位或运维岗位,总会问几道数据库问题。经常有小伙
- 一、将数据写入opengauss前提准备:成功opengauss数据库,并创建用户jack,创建数据库datasets。数据准备:所用数据以
- 直接利用numpy读取非数字型的数据集时需要先进行转换,而且python3在处理中文数据方面确实比较蛋疼。最近在学习周志华老师的那本西瓜书,
- 本文实例为大家分享了js全选操作的具体代码,供大家参考,具体内容如下<html><head><meta htt
- uuid str int 之间的转换import uudi#str 转 uuiduuid.UUID('123456781234567
- 最近在刚从tensorflow转入pytorch,对于自定义的nn.Module 碰到了个问题,即使把模组 modle=Model().cu