当前位置:网站首页>小程序自定义原生底部导航
小程序自定义原生底部导航
2022-04-22 05:57:00 【招来红月】
效果图

app.json 文件添加导航
"custom": true, 开启自定义导航
"tabBar": {
"custom": true,
"color": "#000000",
"selectedColor": "#000000",
"backgroundColor": "rgba(0,0,0,0)",
"list": [
{
"pagePath": "pages/index/index",
"text": "首页"
},
{
"pagePath": "pages/page1/page1",
"text": "page1"
},
{
"pagePath": "pages/page2/page2",
"text": "page2"
},
{
"pagePath": "pages/page3/page3",
"text": "page3"
},
{
"pagePath": "pages/page4/page4",
"text": "page4"
}
]
},
在 app.json同级目录创建文件夹 custom-tab-bar (文件夹位置和名称必须 为custom-tab-bar )
文件夹下创建 index.js index.json index.wxml index.wxss 四个文件 内容就是小程序 自定义组件内容 我的项目结构 image文件夹里面是用到的图标

index.wxml文件
<view class="tab-bar">
<image class="tab-bar-bg" src="./image/t_bg.png" mode="widthFix"></image>
<view class="tab-bar-list">
<view wx:for="{
{list}}" wx:key="index" class="tab-bar-item" data-path="{
{item.pagePath}}" data-index="{
{index}}" bindtap="switchTab">
<image class="tab-bar-item-image" src="{
{selected === index ? item.selectedIconPath : item.iconPath}}" mode="widthFix"></image>
<view class="tab-bar-item-text" style="color: {
{selected === index ? selectedColor : color}}">{
{item.text}}</view>
</view>
</view>
</view>
index.json文件
{
"component": true
}
index.wxss文件
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
z-index: 999;
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
.tab-bar-list {
width: 100%;
display: grid;
grid-template-columns: repeat(5, 1fr);
}
.tab-bar-item {
position: relative;
}
.tab-bar-item-image {
display: block;
margin: 0 auto;
width: 44rpx;
height: auto;
padding-top: 16rpx;
}
.tab-bar-item-text {
padding-top: 5rpx;
padding-bottom: 5rpx;
text-align: center;
font-size: 20rpx;
}
.tab-bar-item:nth-child(3) .tab-bar-item-image {
position: relative;
top: -44rpx;
left: 3rpx;
transform: scale(2);
}
.tab-bar-bg {
position: absolute;
left: 0;
top: 0;
width: 100%;
}
index.js文件
Component({
data: {
selected: 0,
color: "#576271",
selectedColor: "#FF3800",
// 这里的格式和app.json的tabBar格式对应
list: [{
pagePath: "/pages/index/index",
iconPath: "./image/t_01.png",
selectedIconPath: "./image/t_01_active.png",
text: "首页"
}, {
pagePath: "/pages/page1/page1",
iconPath: "./image/t_02.png",
selectedIconPath: "./image/t_02_active.png",
text: "page1"
}, {
pagePath: "/pages/page2/page2",
iconPath: "./image/t_03.png",
selectedIconPath: "./image/t_03.png",
text: "page2"
}, {
pagePath: "/pages/page3/page3",
iconPath: "./image/t_04.png",
selectedIconPath: "./image/t_04_active.png",
text: "page3"
}, {
pagePath: "/pages/page4/page4",
iconPath: "./image/t_05.png",
selectedIconPath: "./image/t_05_active.png",
text: "page4"
}]
},
attached() {},
methods: {
switchTab({
currentTarget
} = e) {
const data = currentTarget.dataset
const url = data.path
wx.switchTab({
url
})
this.setData({
selected: data.index
})
}
}
})
以上都编写好后 在导航对应的页面 添加以下代码
onShow() {
//标记当前页面对应的选中图标
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 0 //该页面对应自定义底部导航的下标
})
}
},
版权声明
本文为[招来红月]所创,转载请带上原文链接,感谢
https://blog.csdn.net/divpoxiao/article/details/121370912
边栏推荐
- PostgreSQL使用clickhousedb_fdw访问ClickHouse
- Use PG_ STAT_ Replication monitoring replication
- 点击触发其他dom元素:< $refs,$el >
- Solve the problem of error in installing PostgreSQL under windows2012 R2
- ES6 modularization
- 携手强化「内容审核」能力,融云与数美科技达成战略合作!
- 数美科技受邀参加信通院内容治理标签研讨会
- 创新实训(二)任务划分
- 创新实训(四)进度
- Mvcc transaction isolation in PostgreSQL
猜你喜欢

Solve the problem of error in installing PostgreSQL under windows2012 R2

Idea插件之RestfulToolkit

创新实训(九)整合

剑指offer:二叉树中和为某一值的路径(回溯)

小程序调用扫描二维码功能并跳转到二维码指定的路径

MYSQL事务之事务隔离级别

MySQL basics 2

A series of interpretations of the general data protection regulations (gdpr): how to judge whether offshore enterprises are under the jurisdiction of gdpr?

Iframe子父级传参

一套sql语句同时支持Oracle跟Mysql?
随机推荐
Using ltree to process hierarchical data in PostgreSQL
企业风控如何搭建四大体系,实现全局防控?
剑指offer:数据流中的中位数(优先队列 大顶堆小顶堆 leetcode 295)
使用Navicat 备份mysql数据库
8张图让你一步步看清 async/await 和 promise 的执行顺序
Open source database management systems are now more popular than commercial products
Sort sort
从零开始学安卓(kotlin)一 ——入门
数美科技与澎湃新闻联合发布《网络信息内容安全洞察报告》
Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: “/?k=“
Great! Kyushu cloud edge computing management platform has been certified by the national authority
MySQL 实现行转列SQL
Pgbouncer最佳实践:系列三
Automatically add partitions for PostgreSQL tables
创新实训(九)整合
Kyushu cloud was selected into the list of 36 krypton enterprises with the most potential to land on the science and innovation board
毕业设计碎碎念
滚动条的多种样式
Pgdoucer best practices: Series 3
Join hands to strengthen the ability of "content audit" and achieve strategic cooperation between rongyun and digital beauty technology!