mirror of
https://github.com/KazooTTT/kazoottt-blog.git
synced 2025-06-23 02:31:33 +08:00
feat: 菜单平滑滚动
This commit is contained in:
@ -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>
|
||||||
|
Reference in New Issue
Block a user