Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions vdoing/components/RightMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'level' + item.level,
{ active: item.slug === hashText },
]"
:id="`${item.slug}-slug`"
v-for="(item, i) in headers"
:key="i"
>
Expand Down Expand Up @@ -86,6 +87,8 @@ export default {
$route() {
this.headers = getNewHeaders(this.$page.headers);
this.getHashText();
// 滚动到指定位置
this.scrollToHash();
},
},
methods: {
Expand All @@ -98,6 +101,11 @@ export default {
this.hashText = this.headers[0]?.slug;
}
},
scrollToHash() {
const element = document.getElementById(`${this.hashText}-slug`);
if (element)
element.scrollIntoView({ behavior: "smooth", block: "center" });
},
},
};
</script>
Expand Down
22 changes: 15 additions & 7 deletions vdoing/layouts/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,7 @@ export default {
},
mounted() {
init();
// 初始化页面时链接锚点无法跳转到指定id的解决方案
const hash = document.location.hash;
if (hash.length > 1) {
const id = decodeURIComponent(hash.substring(1));
const element = document.getElementById(id);
if (element) element.scrollIntoView();
}
this.initView();

// 解决移动端初始化页面时侧边栏闪现的问题
this.showSidebar = true;
Expand Down Expand Up @@ -281,8 +275,22 @@ export default {
themeMode() {
this.setBodyClass();
},
"$route.path"() {
setTimeout(() => {
this.initView();
}, 100);
},
},
methods: {
initView() {
// 初始化页面时链接锚点无法跳转到指定id的解决方案
const hash = document.location.hash;
if (hash.length > 1) {
const id = decodeURIComponent(hash.substring(1));
const element = document.getElementById(id);
if (element) element.scrollIntoView({ behavior: "smooth" });
}
},
getHtmlStr(module) {
const { htmlModules } = this.$themeConfig;
return htmlModules ? htmlModules[module] : "";
Expand Down
Loading