Files
Jiao77-Blog/src/components/TableOfContents.astro
2026-03-01 12:45:33 +08:00

345 lines
7.4 KiB
Plaintext

---
interface Heading {
depth: number;
slug: string;
text: string;
}
interface Props {
headings: Heading[];
}
const { headings } = Astro.props;
// 过滤出 h2 和 h3 标题
const tocItems = headings.filter(h => h.depth >= 2 && h.depth <= 3);
---
<!-- 移动端悬浮按钮 -->
<button
id="toc-toggle"
class="toc-toggle xl:hidden"
aria-label="切换目录"
type="button"
>
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h7" />
</svg>
<span class="ml-1 text-sm font-medium">目录</span>
</button>
<!-- 移动端遮罩层 -->
<div id="toc-overlay" class="toc-overlay xl:hidden"></div>
<!-- 目录导航 -->
<nav id="toc-nav" class="toc-nav" aria-label="文章目录">
<div class="toc-header">
<span class="text-sm font-semibold text-foreground">目录</span>
<button id="toc-close" class="toc-close xl:hidden" aria-label="关闭目录" type="button">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<ul class="toc-list">
{tocItems.map((heading) => (
<li class={`toc-item toc-depth-${heading.depth}`}>
<a
href={`#${heading.slug}`}
class="toc-link"
data-heading-slug={heading.slug}
>
{heading.text}
</a>
</li>
))}
</ul>
</nav>
<style>
/* 移动端悬浮按钮 */
.toc-toggle {
position: fixed;
bottom: 1.5rem;
right: 1.5rem;
z-index: 40;
display: flex;
align-items: center;
padding: 0.75rem 1rem;
background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
color: white;
border: none;
border-radius: 9999px;
box-shadow: 0 4px 14px rgba(59, 130, 246, 0.4);
cursor: pointer;
transition: all 0.2s ease;
}
.toc-toggle:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(59, 130, 246, 0.5);
}
.toc-toggle:active {
transform: translateY(0);
}
/* 目录导航 - 移动端默认隐藏在右侧 */
.toc-nav {
position: fixed;
top: 0;
right: 0;
width: 16rem;
height: 100vh;
background: var(--color-background);
border-left: 1px solid var(--color-border);
padding: 1.5rem;
z-index: 50;
box-shadow: -4px 0 20px rgba(0, 0, 0, 0.15);
transform: translateX(100%);
transition: transform 0.3s ease;
overflow-y: auto;
}
.toc-nav.open {
transform: translateX(0);
}
/* 桌面端 - 右侧 sticky 侧边栏 */
@media (min-width: 1280px) {
.toc-toggle {
display: none;
}
.toc-nav {
position: sticky;
top: 6rem;
right: auto;
width: 12rem;
height: auto;
max-height: calc(100vh - 8rem);
border: none;
background: transparent;
box-shadow: none;
padding: 0;
transform: none;
overflow-y: auto;
}
.toc-close {
display: none;
}
}
/* 目录头部 */
.toc-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1rem;
padding-bottom: 0.75rem;
border-bottom: 1px solid var(--color-border);
}
@media (min-width: 1280px) {
.toc-header {
border-bottom: none;
padding-bottom: 0;
margin-bottom: 0.75rem;
}
}
.toc-close {
padding: 0.25rem;
color: var(--color-muted-foreground);
background: transparent;
border: none;
cursor: pointer;
border-radius: 0.25rem;
transition: all 0.2s;
}
.toc-close:hover {
color: var(--color-foreground);
background: var(--color-muted);
}
/* 目录列表 */
.toc-list {
list-style: none;
padding: 0;
margin: 0;
}
.toc-item {
margin: 0;
}
.toc-link {
display: block;
padding: 0.5rem 0.75rem;
font-size: 0.8125rem;
line-height: 1.4;
color: var(--color-muted-foreground);
text-decoration: none;
border-radius: 0.375rem;
border-left: 2px solid transparent;
transition: all 0.15s ease;
}
.toc-link:hover {
color: var(--color-foreground);
background: var(--color-muted);
}
.toc-link.active {
color: var(--color-primary-500);
border-left-color: var(--color-primary-500);
background: rgba(59, 130, 246, 0.1);
font-weight: 500;
}
/* 缩进层级 */
.toc-depth-3 {
padding-left: 0.5rem;
}
.toc-depth-3 .toc-link {
font-size: 0.75rem;
padding-left: 1rem;
}
/* 遮罩层 */
.toc-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 45;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s ease;
}
.toc-overlay.open {
opacity: 1;
pointer-events: auto;
}
@media (min-width: 1280px) {
.toc-overlay {
display: none;
}
}
/* 滚动条样式 */
.toc-nav::-webkit-scrollbar {
width: 4px;
}
.toc-nav::-webkit-scrollbar-track {
background: transparent;
}
.toc-nav::-webkit-scrollbar-thumb {
background: var(--color-border);
border-radius: 2px;
}
.toc-nav::-webkit-scrollbar-thumb:hover {
background: var(--color-muted-foreground);
}
</style>
<script>
// DOM 元素
const tocToggle = document.getElementById('toc-toggle');
const tocNav = document.getElementById('toc-nav');
const tocClose = document.getElementById('toc-close');
const tocOverlay = document.getElementById('toc-overlay');
const tocLinks = document.querySelectorAll('.toc-link');
// 切换目录显示
function openToc() {
tocNav?.classList.add('open');
tocOverlay?.classList.add('open');
document.body.style.overflow = 'hidden';
}
function closeToc() {
tocNav?.classList.remove('open');
tocOverlay?.classList.remove('open');
document.body.style.overflow = '';
}
// 绑定事件
tocToggle?.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
if (tocNav?.classList.contains('open')) {
closeToc();
} else {
openToc();
}
});
tocClose?.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
closeToc();
});
tocOverlay?.addEventListener('click', closeToc);
// 点击链接后关闭目录(移动端)
tocLinks.forEach(link => {
link.addEventListener('click', () => {
if (window.innerWidth < 1280) {
closeToc();
}
});
});
// 监听窗口大小变化
window.addEventListener('resize', () => {
if (window.innerWidth >= 1280) {
closeToc();
}
});
// ESC 键关闭
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
closeToc();
}
});
// 滚动高亮当前标题
const headings = document.querySelectorAll('article h2[id], article h3[id]');
const observerOptions = {
rootMargin: '-80px 0px -70% 0px',
threshold: 0
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const id = entry.target.id;
tocLinks.forEach(link => {
link.classList.remove('active');
if (link instanceof HTMLAnchorElement) {
const slug = link.getAttribute('data-heading-slug');
if (slug === id) {
link.classList.add('active');
}
}
});
}
});
}, observerOptions);
headings.forEach(heading => {
observer.observe(heading);
});
</script>