>()
+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 @@