From 6c545aa6be03e8890e5cdbb79805a9b68b8c410f Mon Sep 17 00:00:00 2001 From: PeterAlbus Date: Sat, 18 Nov 2023 16:48:33 +0800 Subject: [PATCH] Fix bug in About.vue --- src/views/blog/About.vue | 37 ++++++++++++++++++++----------------- src/views/blog/Blog.vue | 5 ++--- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/views/blog/About.vue b/src/views/blog/About.vue index 985e78f..9735dd4 100644 --- a/src/views/blog/About.vue +++ b/src/views/blog/About.vue @@ -8,7 +8,11 @@

{{blog.blogAuthor}}  发布于{{blog.blogTime}} {{blog.blogViews}}次访问

- +
@@ -19,7 +23,7 @@
>() +const mdRef=ref() const titleList:any=ref([]) const hideCatalogue=ref(true) @@ -110,28 +113,28 @@ const getTitles=()=>{ titleList.value = []; return; } - const hTags = Array.from(new Set(titles.map((title:any) => title.tagName))).sort(); - titleList.value = titles.map((el:any) => ({ title: el.innerText, - lineIndex: el.getAttribute('data-v-md-line'), + id: el.querySelectorAll('a')[0].id, indent: hTags.indexOf(el.tagName), })); + titleList.value = titleList.value.slice(titleList.value.length / 2) } const handleAnchorClick=(anchor:any)=>{ - const { lineIndex } = anchor; - - const heading = mdRef.value?.$el.querySelector(`[data-v-md-line="${lineIndex}"]`); - - if (heading) { - mdRef.value?.scrollToTarget({ - target: heading, - scrollContainer: window, - top: 60, - }); + const target:HTMLElement|null = document.getElementById(anchor.id) + if(!target) return + let offsetTop:number = target.offsetTop || 0; + let parentElement:HTMLElement|null = target.offsetParent as HTMLElement; + while (parentElement) { + offsetTop += parentElement.offsetTop; + parentElement = parentElement.offsetParent as HTMLElement; } + window.scrollTo({ + top: offsetTop - 60, + behavior: 'smooth' + }) } onMounted(()=>{ diff --git a/src/views/blog/Blog.vue b/src/views/blog/Blog.vue index 8623fb9..887534b 100644 --- a/src/views/blog/Blog.vue +++ b/src/views/blog/Blog.vue @@ -23,7 +23,7 @@