网站首页 > 技术文章 正文
一、 效果
二、新建tabbar页面
三 、page.json配置tabbar
"tabBar": {
"color": "#ffffff",
"selectedColor": "#6777FD",
"custom": true,
"list": [{
"pagePath": "pages/index/index",
"text": "首页",
"iconPath": "/static/logo.png",
"selectedIconPath": "/static/logo.png"
},
{
"pagePath": "pages/code/code",
"text": "扫码",
"iconPath": "/static/logo.png",
"selectedIconPath": "/static/logo.png"
},
{
"pagePath": "pages/my/my",
"text": "我的",
"iconPath": "/static/logo.png",
"selectedIconPath": "/static/logo.png"
}
]
},
四、自定义组件
把菜单数据定义在data里,然后遍历渲染
路径
<template>
<view class="tab-bar">
<!-- 循环每个 item 菜单 -->
<!-- 定义两个class,一个是默认的样式tab-bar-item,一个是中间凸起的样式code-->
<view v-for="(item,index) in tabBarList" :key="index" :class="{'tab-bar-item': item.id!=1,code:item.id==1}"
@click="switchTab(item, index)">
<view class="tab_text" :style="{color: selected == index ? selectedColor : color}">
<!-- 图标,当前选中就是selectedIconPath,否则就是默认的iconPath-->
<image class="tab_img" :src="selected == index ? item.selectedIconPath : item.iconPath"></image>
<!-- 中间菜单不显示文字,index不是1的显示文字-->
<template v-if="index!==1">
<view>{{ item.text }}</view>
</template>
</view>
</view>
</view>
</template>
<script setup>
import { defineProps, ref } from 'vue'
// 子组件传递参数
const props = defineProps({
selected: {
type: Number,
default: 0
}
})
// 默认颜色
let color = ref('#000')
// 选中的颜色
let selectedColor = ref('#ffb2b2')
// 菜单栏集合 - 与 pages.json -> tabbar 配置一样
let tabBarList = ref([{
"id": 0,
"pagePath": "/pages/index/index",
"iconPath": "../../static/logo.png",
"selectedIconPath": "../../static/logo.png",
"text": "首页"
},
{
"id": 1,
"pagePath": "/pages/code/code",
"iconPath": "../../static/logo.png",
"selectedIconPath": "../../static/logo.png",
"text": "扫码"
},
{
"id": 2,
"pagePath": "/pages/my/my",
"iconPath": "../../static/logo.png",
"selectedIconPath": "../../static/logo.png",
"text": "我的"
}
])
// 隐藏原生TabBar
uni.hideTabBar();
// 跳转tabBar菜单栏
const switchTab = (item) => {
let url = item.pagePath;
uni.switchTab({
url
})
}
</script>
<style lang="less" scoped>
// 外部装修
.tab-bar {
position: fixed;
bottom: 0;
left: 0rpx;
right: 0rpx;
height: 100rpx;
background: white;
padding: 10rpx;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 4px 15px rgba(165, 168, 171, 0.83) !important;
// 给每个 item 设置样式
.tab-bar-item {
//flex: 0.5;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
width: 150rpx;
padding: 15rpx;
background-color: transparent;
transition: all 0.5s ease-in-out;
margin: auto;
// 限制每个icon的大小
.tab_img {
width: 37rpx;
height: 41rpx;
}
// 限制文字大小
.tab_text {
font-size: 20rpx;
margin-top: 9rpx;
flex: 1;
}
}
//中间凸起扫码
.code {
position: relative;
top: -60rpx;
width: 120rpx;
height: 120rpx;
border-radius: 60rpx;
background-color: #2b9939;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
// 限制每个icon的大小
.tab_img {
width: 70rpx;
height: 78rpx;
}
}
}
</style>
五、组件中使用
index.vue
<template>
index
<tabbar selected="0"></tabbar>
</template>
<script>
import tabbar from "../../components/tabbar/tabbar.vue";
</script>
<style>
</style>
code.vue
<template>
code
<tabbar selected="1"></tabbar>
</template>
<script setup>
import tabbar from "../../components/tabbar/tabbar.vue";
</script>
<style lang="scss">
</style>
my.vue
<template>
index<tabbar selected="2"></tabbar>
</template>
<script>
import tabbar from "../../components/tabbar/tabbar.vue";
</script>
<style lang="scss">
</style>
猜你喜欢
- 2025-09-21 提升档次的CSS伪元素技巧!原来::before和::after还能这么玩
- 2025-09-21 深入理解CSS变量(Custom Properties)
- 2025-09-21 PLC 数据采集 + MySQL 存储 + Flask+ECharts 可视化系统
- 2025-09-21 使用deepSeek生成一个贪吃蛇游戏_canvas 贪吃蛇
- 2025-09-21 CSS 电梯:纯 CSS 实现的状态机与楼层导航
- 2025-09-21 操作系统应用开发(四)压缩软件开发demo——东方仙盟筑基期
- 2025-09-21 前端学习核心_黑马程序员vue全套视频教程从vue2 0到vue3 0一套全覆盖前端学习核心框架教程
- 2025-09-21 操作系统应用开发(七)mac苹果模拟器——东方仙盟练气期
- 2025-09-21 档案系统(一)学员管理系统开发——东方仙盟筑基期
- 2024-08-05 用多了各种组件库的你还会用原生JS写轮播图吗?
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)