Skip to content

Commit 5eec53b

Browse files
committed
feat: auto scroll to target sidebar
1 parent 697f6e4 commit 5eec53b

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

docs/.vitepress/theme/index.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import './style/global.css'
1414
import '@nolebase/vitepress-plugin-enhanced-readabilities/client/style.css'
1515
import VersionSwitcher from "./components/VersionSwitcher.vue";
1616

17+
// noinspection JSUnusedGlobalSymbols
1718
export default {
1819
extends: DefaultTheme,
1920
enhanceApp({app}) {
@@ -34,16 +35,25 @@ export default {
3435
return h(DefaultTheme.Layout, null, children);
3536
},
3637
setup() {
37-
const route = useRoute()
38-
const initZoom = () => {
39-
mediumZoom('.main img', {background: 'var(--vp-c-bg)'})
40-
}
41-
onMounted(() => {
38+
const route = useRoute();
39+
onMounted(async () => {
4240
initZoom()
43-
})
44-
watch(
45-
() => route.path,
46-
() => nextTick(() => initZoom())
47-
)
41+
await nextTick(() => scrollToActiveSidebarItem())
42+
});
43+
watch(() => route.path, () => nextTick(() => {
44+
scrollToActiveSidebarItem()
45+
initZoom()
46+
}));
47+
}
48+
} satisfies Theme
49+
50+
function initZoom() {
51+
mediumZoom('.main img', {background: 'var(--vp-c-bg)'})
52+
}
53+
54+
function scrollToActiveSidebarItem() {
55+
const activeLink = document.querySelector('#VPSidebarNav div.is-link.is-active.has-active')
56+
if (activeLink) {
57+
activeLink.scrollIntoView({behavior: 'smooth', block: 'center'})
4858
}
49-
} satisfies Theme
59+
}

0 commit comments

Comments
 (0)