Files
schoolNews/schoolNewsWeb/src/components/base/FloatingSidebar.vue

174 lines
3.1 KiB
Vue
Raw Normal View History

2025-10-17 12:05:04 +08:00
<template>
2025-10-18 18:19:19 +08:00
<aside class="floating-sidebar">
2025-10-17 12:05:04 +08:00
<!-- 侧边栏内容 -->
2025-10-18 18:19:19 +08:00
<div class="sidebar-content">
2025-10-17 12:05:04 +08:00
<!-- 菜单列表 -->
<nav class="sidebar-nav">
<div
v-for="menu in menus"
2025-10-18 18:19:19 +08:00
:key="menu.menuID || menu.url"
2025-10-17 12:05:04 +08:00
class="sidebar-item"
2025-10-18 18:19:19 +08:00
:class="{ active: isActive(menu) }"
@click="handleClick(menu)"
2025-10-17 12:05:04 +08:00
>
2025-10-18 18:19:19 +08:00
<div class="sidebar-link">
2025-10-17 12:05:04 +08:00
<span class="link-text">{{ menu.name }}</span>
</div>
</div>
</nav>
</div>
</aside>
</template>
<script setup lang="ts">
import type { SysMenu } from '@/types';
2025-10-18 18:19:19 +08:00
import { useRouter, useRoute } from 'vue-router';
2025-10-17 12:05:04 +08:00
// Props
interface Props {
menus: SysMenu[];
activePath?: string;
}
2025-10-18 18:19:19 +08:00
const props = withDefaults(defineProps<Props>(), {});
2025-10-17 12:05:04 +08:00
// Emits
const emit = defineEmits<{
'menu-click': [menu: SysMenu];
}>();
2025-10-18 18:19:19 +08:00
const router = useRouter();
const route = useRoute();
2025-10-17 12:05:04 +08:00
// 检查菜单是否激活
function isActive(menu: SysMenu): boolean {
2025-10-18 18:19:19 +08:00
if (props.activePath) {
return props.activePath === menu.url;
}
2025-10-17 12:05:04 +08:00
if (!menu.url) return false;
2025-10-18 18:19:19 +08:00
return route.path === menu.url;
2025-10-17 12:05:04 +08:00
}
// 处理点击
function handleClick(menu: SysMenu) {
// 触发点击事件
emit('menu-click', menu);
2025-10-18 18:19:19 +08:00
// 如果有URL进行路由跳转
if (menu.url) {
router.push(menu.url);
}
2025-10-17 12:05:04 +08:00
}
</script>
<style lang="scss" scoped>
.floating-sidebar {
2025-10-18 18:19:19 +08:00
width: 180px;
background: #FFFFFF;
border-radius: 10px;
2025-10-17 12:05:04 +08:00
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
flex-shrink: 0;
display: flex;
flex-direction: column;
position: relative;
}
.sidebar-content {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
.sidebar-nav {
flex: 1;
overflow-y: auto;
2025-10-18 18:19:19 +08:00
padding: 20px 0;
display: flex;
flex-direction: column;
gap: 20px;
2025-10-17 12:05:04 +08:00
/* 滚动条样式 */
&::-webkit-scrollbar {
2025-10-18 18:19:19 +08:00
width: 4px;
2025-10-17 12:05:04 +08:00
}
&::-webkit-scrollbar-track {
2025-10-18 18:19:19 +08:00
background: transparent;
2025-10-17 12:05:04 +08:00
}
&::-webkit-scrollbar-thumb {
background: #ddd;
2025-10-18 18:19:19 +08:00
border-radius: 2px;
2025-10-17 12:05:04 +08:00
&:hover {
background: #bbb;
}
}
}
.sidebar-item {
2025-10-18 18:19:19 +08:00
position: relative;
height: 54px;
2025-10-17 12:05:04 +08:00
display: flex;
align-items: center;
cursor: pointer;
2025-10-18 18:19:19 +08:00
transition: all 0.2s;
2025-10-17 12:05:04 +08:00
2025-10-18 18:19:19 +08:00
&::before {
content: '';
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
width: 4px;
height: 20px;
background: transparent;
transition: background 0.2s;
2025-10-17 12:05:04 +08:00
}
2025-10-18 18:19:19 +08:00
&.active {
&::before {
background: #C62828;
}
2025-10-17 12:05:04 +08:00
2025-10-18 18:19:19 +08:00
.sidebar-link {
color: #C62828;
}
2025-10-17 12:05:04 +08:00
}
&:hover {
2025-10-18 18:19:19 +08:00
.sidebar-link {
color: #C62828;
}
2025-10-17 12:05:04 +08:00
}
}
2025-10-18 18:19:19 +08:00
.sidebar-link {
2025-10-17 12:05:04 +08:00
flex: 1;
display: flex;
align-items: center;
justify-content: center;
2025-10-18 18:19:19 +08:00
height: 100%;
padding: 0 16px;
color: #334155;
font-size: 16px;
font-weight: 500;
font-family: 'PingFang SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
line-height: 1.5;
transition: color 0.2s;
user-select: none;
2025-10-17 12:05:04 +08:00
2025-10-18 18:19:19 +08:00
.link-text {
text-align: center;
2025-10-17 12:05:04 +08:00
}
}
/* 响应式设计 */
@media (max-width: 768px) {
.floating-sidebar {
2025-10-18 18:19:19 +08:00
width: 160px;
2025-10-17 12:05:04 +08:00
}
}
</style>