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

220 lines
4.0 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-20 18:28:38 +08:00
:class="{
active: isActive(menu),
'theme-resource': props.theme === 'resource'
}"
2025-10-18 18:19:19 +08:00
@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-20 18:28:38 +08:00
theme?: 'default' | 'resource'; // 新增主题属性
2025-10-17 12:05:04 +08:00
}
2025-10-20 18:28:38 +08:00
const props = withDefaults(defineProps<Props>(), {
theme: 'default'
});
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-20 18:28:38 +08:00
transition: all 0.3s;
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-20 18:28:38 +08:00
// Resource theme - 红色背景高亮
&.theme-resource {
padding: 0 10px;
&:hover {
background: rgba(198, 40, 40, 0.05);
}
&.active {
background: #C62828;
border-radius: 8px;
margin: 5px 10px;
height: 44px;
&::before {
display: none; // 隐藏左侧指示条
}
.sidebar-link {
color: #FFFFFF;
}
&::after {
content: '';
position: absolute;
right: -103px;
top: 3px;
width: 48.53px;
height: 50.54px;
background: linear-gradient(134deg, rgba(255, 255, 255, 1) 26%, rgba(255, 255, 255, 0) 86%);
opacity: 0.23;
pointer-events: none;
}
}
}
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;
2025-12-01 18:37:43 +08:00
font-family: 'Source Han Sans SC', sans-serif;
2025-10-18 18:19:19 +08:00
line-height: 1.5;
2025-10-20 18:28:38 +08:00
transition: color 0.3s;
2025-10-18 18:19:19 +08:00
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
}
2025-10-20 18:28:38 +08:00
.theme-resource & {
margin-left: 32px; // Resource theme 文字左移
}
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>