feat: 菜单平滑滚动

This commit is contained in:
KazooTTT
2024-10-15 12:49:56 +08:00
parent 11422621b1
commit f2a7ecc339

View File

@ -13,8 +13,9 @@ const {
<li class={`${depth > 2 ? 'ms-2' : ''}`}> <li class={`${depth > 2 ? 'ms-2' : ''}`}>
<a <a
aria-label={`Scroll to section: ${text}`} aria-label={`Scroll to section: ${text}`}
class={`block line-clamp-2 ${depth <= 2 ? 'mt-2' : 'mt-1 text-sm'} text-foreground/75 transition-all hover:text-foreground`} class={`block line-clamp-2 ${depth <= 2 ? 'mt-2' : 'mt-1 text-sm'} text-foreground/75 transition-all hover:text-foreground toc-link`}
href={`#${slug}`}>{text}</a href={`#${slug}`}
data-slug={slug}>{text}</a
> >
{ {
!!subheadings.length && ( !!subheadings.length && (
@ -26,3 +27,19 @@ const {
) )
} }
</li> </li>
<script>
document.addEventListener('DOMContentLoaded', () => {
const tocLinks = document.querySelectorAll('.toc-link')
tocLinks.forEach((link) => {
link.addEventListener('click', (event) => {
event.preventDefault()
const slug = link.getAttribute('data-slug')
const element = document.getElementById(slug)
if (element) {
element.scrollIntoView({ behavior: 'smooth' })
}
})
})
})
</script>