当前位置:网站首页>微信小程序封装自定义tabbar,子页面显示tabbar,主页面也可设置(建议使用小程序原有tabbar),仅个人使用
微信小程序封装自定义tabbar,子页面显示tabbar,主页面也可设置(建议使用小程序原有tabbar),仅个人使用
2022-07-19 05:06:00 【kristen999】
最近刚开始着手写小程序,无奈太菜了,实在是搞不出来,就只能从网上东拼西凑一个,目前看来算是一个还不错的tabbar,留着自用;
最终实现效果:主页面
子页面
1、首先新建一个组件,用来存放公用的tabbar
具体新建过程:目录根部右击创建文件夹components,再右击components =>新建文件夹 ,然后定义名字为tabbar,右击tabbar =>创建组件,tabbar组件便创建成功
2、组件创建好后,开始编写具体实现功能代码
wxml:
<cover-view class="tab-bar">
<cover-view class="tab-bar-border"></cover-view>
<cover-view wx:for="{
{list}}" wx:key="index" class="section_item" data-path="{
{item.pagePath}}" data-index="{
{index}}" bindtap="switchTab">
<cover-image class="section_image" src="{
{selected == index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view class="section_title" style="color: {
{selected == index ? selectedColor : color}}">{
{item.text}}</cover-view>
</cover-view>
</cover-view>
wxss: (和微信自带tabbar样式几乎一样)
/* components/custom-tab-bar/tabbar.wxss */
.tab-bar {
width: 100%;
height: 60px;
position: fixed;
bottom: 0;
background: #f2f2f2;
display: flex;
z-index: 999;
border-top:1px solid #dddddd
}
.section_item {
flex: 1;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
padding: 5px 0 ;
}
.section_item .section_image {
width: 29px;
height: 26px;
}
.section_item .section_title {
font-size: 13px;
font-weight:300;
}
json:
{
"component": true,
"usingComponents": {}
}
js:
Component({
// 组件的属性列表
properties: {
// 接受父组件的给的数据
active: {
type: "String",
value: ""
}
},
data: {
selected: undefined,
color: "#333333", //默认文字颜色
selectedColor: "#e6a511", //选中文字颜色
list: [
{
"text":"门禁",
"pagePath":"../../pages/access/access",
"iconPath":"/images/access.png",
"selectedIconPath":"/images/accessclick.png"
},
{
"text":"拼团",
"pagePath":"../../pages/group/group",
"iconPath":"/images/group.png",
"selectedIconPath":"/images/groupclick.png"
},
{
"text":"首页",
"pagePath":"../../pages/home/home",
"iconPath":"/images/home.png",
"selectedIconPath":"/images/homeclick.png"
},
{
"text":"待办",
"pagePath":"../../pages/todo/todo",
"iconPath":"/images/todo.png",
"selectedIconPath":"/images/todoclick.png"
},
{
"text":"我的",
"pagePath":"../../pages/user/user",
"iconPath":"/images/user.png",
"selectedIconPath":"/images/userclick.png"
}
]
},
attached() {
},
methods: {
switchTab(e) {
if (this.data.selected === e.currentTarget.dataset.index) {
return false;
} else {
const url=e.currentTarget.dataset.path
wx.switchTab({url})
}
},
run() {
console.log(this.data.active);
this.setData({
// 通过this.data获取父组件里传过来的值
selected: this.data.active
});
console.log(this.data.selected);
}
}
})
3、页面进行引用
我的页面是notice页面,所以拿notice举个例子
json:
主要是将tabbar引入就可以
{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#f2f2f2",
"navigationBarTextStyle":"black",
"backgroundColor":"#f2f2f2",
"navigationStyle": "custom",
"usingComponents": {
"date-time-picker": "../../components/date-time-picker/date-time-picker",
"tabbar":"../../components/tabbar/tabbar"
}
}
然后就可以在wxml中引入
设置js
onShow: function () {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: null //标亮的导航按钮的下标,如果是其他子页面,可以赋值为null
})
}
},
这下子页面引入tabbar就完成了,具体效果图如下:
如果主页面也想要更改小程序自带的tabbar,可以在app.json中添加一句话:"custom":true
然后再首页中引入tabbar,具体步骤同上,也是先写json
这里拿home举例:
json:
wxml:(active是你要点亮的图片和文字的效果)
js:
在onload中写:
引入完成,查看效果
边栏推荐
- 轻松掌握|struct结构体|知识点
- 53 sessionstorage session storage
- 4.MVC模型和MVVM模型
- 77-全局自定义指令
- Explain the full permutation problem in detail (different output results of different solutions)
- Easier to use C language entry-level cheese (2) select statements + loop statements, functions, arrays, operators (super detailed)
- 6.双向绑定(v-model)的实现
- 60-[重点]Object.defineProperty的set方法
- C language custom types: structure, enumeration, union
- 数据在内存中的存储
猜你喜欢
100-京东导航栏-插槽使用-弹性布局(display: flex;)
What if the game needs to be reinstalled after the steam folder is moved
【学习笔记】Unreal(虚幻)4引擎入门(三)
指针数组和数组指针有什么区别?
Standard IO and file IO
The longest common subsequence of order 2 and order n
【学习笔记】Solid Works 作业记录
如何解决跨域问题
找免费录屏软件的过程-没想到win10自带这个功能
枚举(enum)奇妙的使用、联合体(共用体union)对空间节省的巧妙
随机推荐
Special topic of structure
2022-07-18 第五小组 修身课 学习笔记(every day)
宏任务、微任务 和 事件循环机制
进程与线程以及进线程间通信
Character functions and string functions
15. Built in instructions
Pytorch target detection data enhances the mixing of cutmix and mixup
Easier to use C language entry-level cheese (3) common keywords +define+ pointer + structure (super detailed)
结构体专题
Triangle problem worst case test case
C language to achieve basic version of mine sweeping
Part 59: main c:62:9: note: use option -std=c99 or -std=gnu99 to compile your code
【学习笔记】“STL演讲比赛流程管理系统”作业总结
长篇详解C语言操作符
52 localstorage local storage
Development of dynamic memory in C language
6. Implementation of bidirectional binding (V-model)
页面搜索高亮功能,页面自动匹配关键字滚动到屏幕正中间,并高亮显示。
How is data stored in memory?
Space saving "bit segments" in C language“