223 lines
6.3 KiB
Text
223 lines
6.3 KiB
Text
|
|
---
|
||
|
|
const navItems = [
|
||
|
|
{ href: '/', label: '首页', match: 'index' },
|
||
|
|
{ href: '/posts', label: '文章', match: 'posts' },
|
||
|
|
{ href: '/tags', label: '标签', match: 'tags' },
|
||
|
|
{ href: '/archives', label: '归档', match: 'archives' },
|
||
|
|
{ href: '/about', label: '关于', match: 'about' },
|
||
|
|
];
|
||
|
|
|
||
|
|
const path = Astro.url.pathname.replace(/\/+$/, '').replace(/^\//, '');
|
||
|
|
const current = path === '' ? 'index' : path.split('/')[0];
|
||
|
|
---
|
||
|
|
|
||
|
|
<header id="nav" class="nav glass" data-scrolled="0">
|
||
|
|
<div class="container nav-inner">
|
||
|
|
<a href="/" class="brand">
|
||
|
|
<span class="brand-mark">Y</span>
|
||
|
|
<span class="brand-text">Yukun<span class="apos">’</span>s Blog</span>
|
||
|
|
</a>
|
||
|
|
|
||
|
|
<nav class="nav-links" aria-label="主导航">
|
||
|
|
{
|
||
|
|
navItems.map((item) => (
|
||
|
|
<a
|
||
|
|
href={item.href}
|
||
|
|
class:list={['nav-link', { active: current === item.match }]}
|
||
|
|
aria-current={current === item.match ? 'page' : undefined}
|
||
|
|
>
|
||
|
|
{item.label}
|
||
|
|
</a>
|
||
|
|
))
|
||
|
|
}
|
||
|
|
</nav>
|
||
|
|
|
||
|
|
<button id="search-trigger" class="nav-search" aria-label="搜索文章" title="搜索 (Ctrl+K)">
|
||
|
|
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round">
|
||
|
|
<circle cx="11" cy="11" r="7" />
|
||
|
|
<line x1="21" y1="21" x2="16.5" y2="16.5" />
|
||
|
|
</svg>
|
||
|
|
<span class="kbd">Ctrl K</span>
|
||
|
|
</button>
|
||
|
|
|
||
|
|
<button id="menu-toggle" class="menu-toggle" aria-label="菜单" aria-expanded="false">
|
||
|
|
<span></span><span></span><span></span>
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div id="mobile-menu" class="mobile-menu" data-open="0">
|
||
|
|
{navItems.map((item) => (
|
||
|
|
<a href={item.href} class:list={['mobile-link', { active: current === item.match }]}>{item.label}</a>
|
||
|
|
))}
|
||
|
|
<button id="search-trigger-mobile" class="mobile-search">搜索文章</button>
|
||
|
|
</div>
|
||
|
|
</header>
|
||
|
|
|
||
|
|
<style>
|
||
|
|
.nav {
|
||
|
|
position: fixed;
|
||
|
|
top: 0;
|
||
|
|
left: 0;
|
||
|
|
right: 0;
|
||
|
|
z-index: 90;
|
||
|
|
height: var(--nav-h);
|
||
|
|
display: flex;
|
||
|
|
align-items: center;
|
||
|
|
background: transparent;
|
||
|
|
border: 0;
|
||
|
|
box-shadow: none;
|
||
|
|
transition: background 0.35s, box-shadow 0.35s, border-color 0.35s, backdrop-filter 0.35s;
|
||
|
|
}
|
||
|
|
.nav[data-scrolled='1'] {
|
||
|
|
background: var(--glass);
|
||
|
|
backdrop-filter: blur(22px) saturate(160%);
|
||
|
|
-webkit-backdrop-filter: blur(22px) saturate(160%);
|
||
|
|
border-bottom: 1px solid var(--glass-border);
|
||
|
|
box-shadow: 0 4px 24px rgba(31, 96, 160, 0.08);
|
||
|
|
}
|
||
|
|
.nav-inner {
|
||
|
|
display: flex;
|
||
|
|
align-items: center;
|
||
|
|
justify-content: space-between;
|
||
|
|
gap: 16px;
|
||
|
|
height: 100%;
|
||
|
|
}
|
||
|
|
.brand {
|
||
|
|
display: flex;
|
||
|
|
align-items: center;
|
||
|
|
gap: 10px;
|
||
|
|
font-family: var(--font-display);
|
||
|
|
font-weight: 800;
|
||
|
|
font-size: 1.1rem;
|
||
|
|
color: var(--ink);
|
||
|
|
letter-spacing: -0.02em;
|
||
|
|
}
|
||
|
|
.brand:hover { color: var(--ink); }
|
||
|
|
.brand-mark {
|
||
|
|
display: grid;
|
||
|
|
place-items: center;
|
||
|
|
width: 30px; height: 30px;
|
||
|
|
border-radius: 9px;
|
||
|
|
background: linear-gradient(135deg, var(--blue-500), var(--cyan-400));
|
||
|
|
color: #fff;
|
||
|
|
font-size: 1rem;
|
||
|
|
box-shadow: 0 4px 12px rgba(47, 127, 224, 0.4);
|
||
|
|
}
|
||
|
|
.brand-text .apos { color: var(--blue-400); }
|
||
|
|
.nav-links {
|
||
|
|
display: flex;
|
||
|
|
align-items: center;
|
||
|
|
gap: 4px;
|
||
|
|
margin-left: auto;
|
||
|
|
}
|
||
|
|
.nav-link {
|
||
|
|
position: relative;
|
||
|
|
padding: 8px 14px;
|
||
|
|
border-radius: 999px;
|
||
|
|
font-weight: 600;
|
||
|
|
font-size: 0.95rem;
|
||
|
|
color: var(--ink-soft);
|
||
|
|
transition: color 0.2s, background 0.2s;
|
||
|
|
}
|
||
|
|
.nav-link:hover { color: var(--blue-600); background: rgba(132, 194, 255, 0.14); }
|
||
|
|
.nav-link.active { color: var(--blue-600); background: rgba(132, 194, 255, 0.2); }
|
||
|
|
.nav-link.active::after {
|
||
|
|
content: '';
|
||
|
|
position: absolute;
|
||
|
|
left: 50%;
|
||
|
|
bottom: 1px;
|
||
|
|
transform: translateX(-50%);
|
||
|
|
width: 5px; height: 5px;
|
||
|
|
border-radius: 50%;
|
||
|
|
background: var(--blue-500);
|
||
|
|
}
|
||
|
|
.nav-search {
|
||
|
|
display: inline-flex;
|
||
|
|
align-items: center;
|
||
|
|
gap: 8px;
|
||
|
|
padding: 7px 12px;
|
||
|
|
border-radius: 999px;
|
||
|
|
border: 1px solid var(--glass-border);
|
||
|
|
background: rgba(255, 255, 255, 0.4);
|
||
|
|
color: var(--ink-soft);
|
||
|
|
cursor: pointer;
|
||
|
|
transition: all 0.2s;
|
||
|
|
}
|
||
|
|
.nav-search:hover { border-color: var(--blue-300); color: var(--blue-600); }
|
||
|
|
.kbd {
|
||
|
|
font-size: 0.72rem;
|
||
|
|
font-family: var(--font-mono);
|
||
|
|
padding: 2px 6px;
|
||
|
|
border-radius: 5px;
|
||
|
|
background: rgba(132, 194, 255, 0.18);
|
||
|
|
color: var(--blue-700);
|
||
|
|
}
|
||
|
|
.menu-toggle {
|
||
|
|
display: none;
|
||
|
|
flex-direction: column;
|
||
|
|
gap: 5px;
|
||
|
|
padding: 10px;
|
||
|
|
background: none;
|
||
|
|
border: 0;
|
||
|
|
cursor: pointer;
|
||
|
|
}
|
||
|
|
.menu-toggle span {
|
||
|
|
width: 22px; height: 2px;
|
||
|
|
background: var(--ink);
|
||
|
|
border-radius: 2px;
|
||
|
|
transition: transform 0.3s, opacity 0.3s;
|
||
|
|
}
|
||
|
|
.mobile-menu {
|
||
|
|
display: none;
|
||
|
|
flex-direction: column;
|
||
|
|
gap: 4px;
|
||
|
|
padding: 12px 16px 16px;
|
||
|
|
}
|
||
|
|
.mobile-link {
|
||
|
|
padding: 12px 14px;
|
||
|
|
border-radius: 12px;
|
||
|
|
font-weight: 600;
|
||
|
|
color: var(--ink);
|
||
|
|
}
|
||
|
|
.mobile-link.active { background: rgba(132, 194, 255, 0.2); color: var(--blue-600); }
|
||
|
|
.mobile-search {
|
||
|
|
margin-top: 6px;
|
||
|
|
padding: 12px 14px;
|
||
|
|
border-radius: 12px;
|
||
|
|
border: 1px solid var(--glass-border);
|
||
|
|
background: rgba(255, 255, 255, 0.5);
|
||
|
|
text-align: left;
|
||
|
|
cursor: pointer;
|
||
|
|
color: var(--ink-soft);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* 移动端:折叠菜单 */
|
||
|
|
@media (max-width: 768px) {
|
||
|
|
.nav-links, .nav-search { display: none; }
|
||
|
|
.menu-toggle { display: flex; }
|
||
|
|
.nav[data-scrolled='1'] .mobile-menu,
|
||
|
|
.mobile-menu[data-open='1'] {
|
||
|
|
display: flex;
|
||
|
|
border-top: 1px solid var(--glass-border);
|
||
|
|
background: var(--glass-strong);
|
||
|
|
backdrop-filter: blur(22px) saturate(160%);
|
||
|
|
-webkit-backdrop-filter: blur(22px) saturate(160%);
|
||
|
|
}
|
||
|
|
.menu-toggle[aria-expanded='true'] span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
|
||
|
|
.menu-toggle[aria-expanded='true'] span:nth-child(2) { opacity: 0; }
|
||
|
|
.menu-toggle[aria-expanded='true'] span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }
|
||
|
|
.mobile-menu[data-open='0'] { display: none; }
|
||
|
|
}
|
||
|
|
</style>
|
||
|
|
|
||
|
|
<script>
|
||
|
|
// 菜单展开
|
||
|
|
const toggle = document.getElementById('menu-toggle');
|
||
|
|
const menu = document.getElementById('mobile-menu');
|
||
|
|
toggle?.addEventListener('click', () => {
|
||
|
|
const open = menu?.dataset.open === '1';
|
||
|
|
menu!.dataset.open = open ? '0' : '1';
|
||
|
|
toggle!.setAttribute('aria-expanded', String(!open));
|
||
|
|
});
|
||
|
|
</script>
|