Files
gym-manage/gym-manage-uniapp/components/TabBar.vue
T
2026-06-04 13:16:09 +08:00

140 lines
3.4 KiB
Vue

<template>
<!-- 底部导航栏容器 -->
<view class="tab-bar">
<!-- 导航栏项 -->
<view
v-for="(tab, index) in tabs"
:key="index"
:class="['tab-item', { active: currentActive === index }]"
@click="switchTab(index)"
>
<!-- 导航栏图标 -->
<image :src="currentActive === index ? tab.iconActive : tab.icon" mode="aspectFit" class="tab-icon" />
<!-- 导航栏标签文字 -->
<text class="tab-label">{{ tab.label }}</text>
</view>
</view>
</template>
<script setup>
import { ref, watch } from 'vue'
// 当前激活的导航栏索引(从外部传入)
const props = defineProps({
activeTab: {
type: Number,
default: 0
}
})
// 当前激活状态
const currentActive = ref(props.activeTab)
// 监听外部传入的激活状态变化
watch(() => props.activeTab, (newVal) => {
currentActive.value = newVal
})
// 导航栏数据列表
const tabs = [
{
icon: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/home.png',
iconActive: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/active/home.png',
label: '首页',
path: '/pages/index/index'
},
{
icon: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/course.png',
iconActive: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/active/course.png',
label: '课程',
path: '/pages/groupCourse/list'
},
{
icon: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/train.png',
iconActive: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/active/train.png',
label: '训练',
path: '/pages/train/index'
},
{
icon: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/discover.png',
iconActive: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/active/discover.png',
label: '发现',
path: '/pages/discover/index'
},
{
icon: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/profile.png',
iconActive: 'https://gymfuture.oss-cn-chengdu.aliyuncs.com/static/tabBar/active/profile.png',
label: '我的',
path: '/pages/profile/index'
}
]
// 切换标签页
const switchTab = (index) => {
currentActive.value = index
const tab = tabs[index]
// 获取当前页面路径
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
const currentPath = '/' + currentPage.route
// 如果点击的是当前页面,不跳转
if (currentPath === tab.path) {
return
}
// 跳转对应页面
uni.redirectTo({
url: tab.path
})
}
</script>
<style lang="scss" scoped>
/* 底部导航栏容器样式 */
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 120rpx;
background: #1A4A6F;
display: flex;
justify-content: space-around;
align-items: center;
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.06);
border-radius: 32rpx 32rpx 0 0;
z-index: 100;
}
/* 导航栏项样式 */
.tab-item {
display: flex;
flex-direction: column;
align-items: center;
gap: 8rpx;
padding: 12rpx 24rpx;
}
/* 导航栏图标样式 */
.tab-icon {
width: 40rpx;
height: 40rpx;
}
/* 导航栏标签文字样式 */
.tab-label {
font-size: 22rpx;
color: #94a3b8;
}
/* 导航栏激活状态文字样式 */
.tab-item.active .tab-label {
color: #f97316;
font-weight: 600;
}
</style>