Skip to content

Commit 4d56a4d

Browse files
author
潘卓然ParnDeedlit
committed
【站点】【修复】【修复开发指南&产品说明的帮助文档的联动】
1 parent e0ac0d4 commit 4d56a4d

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

website/src/views/component/product.vue

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export default {
6565
markdown: "> `暂无说明`, 请检查该目录下的帮助说明是否存在",
6666
mode: "",
6767
isContentFinish: false,
68-
isTocFinish: false
68+
isTocFinish: false,
69+
observer: undefined
6970
}
7071
},
7172
components: {
@@ -80,34 +81,53 @@ export default {
8081
},
8182
watch: {
8283
"$route.path"() {
84+
this.clearScroll();
8385
this.initConfig();
86+
this.initScroll();
8487
this.changeTabBackground();
8588
}
8689
},
8790
methods: {
91+
clearScroll() {
92+
let {observer} = this;
93+
if(observer) {
94+
document.querySelectorAll('h2[id]').forEach((section) => {
95+
observer.unobserve(section);
96+
});
97+
document.querySelectorAll('h3[id]').forEach((section) => {
98+
observer.unobserve(section);
99+
});
100+
observer.disconnect();
101+
}
102+
},
88103
initScroll() {
104+
const vm = this;
89105
/**
90106
* 目标元素和视窗viewport的交集的变化的方法API
91107
* @type {IntersectionObserver}
92108
*/
93-
const observer = new IntersectionObserver((entries) => {
109+
this.observer = new IntersectionObserver((entries) => {
94110
entries.forEach((entry) => {
95111
let id = entry.target.getAttribute('id');
96112
id = encodeURI(id);
97113
//intersectionRatio:目标元素出现在视窗的比例
98114
if (entry.intersectionRatio > 0) {
99-
document.querySelector(`li a[href="#${id}"]`).parentElement.classList.add('active');
115+
if (document.querySelector(`li a[href="#${id}"]`)) {
116+
document.querySelector(`li a[href="#${id}"]`).parentElement.classList.add('active');
117+
}
100118
} else {
101-
document.querySelector(`li a[href="#${id}"]`).parentElement.classList.remove('active');
119+
if (document.querySelector(`li a[href="#${id}"]`)) {
120+
document.querySelector(`li a[href="#${id}"]`).parentElement.classList.remove('active');
121+
}
102122
}
103123
});
104124
});
105125
//observer.observe():监听当前所有的h2[id]
106126
document.querySelectorAll('h2[id]').forEach((section) => {
107-
observer.observe(section);
127+
vm.observer.observe(section);
108128
});
109129
document.querySelectorAll('h3[id]').forEach((section) => {
110-
observer.observe(section);
130+
vm.observer.observe(section);
111131
});
112132
},
113133
initConfig() {

website/src/views/guide/DevelopGuide.vue

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export default {
6464
markdown: '> `暂无说明`, 请检查该目录下的帮助说明是否存在',
6565
isContentFinish: false,
6666
isTocFinish: false,
67-
mode: ""
67+
mode: "",
68+
observer: undefined
6869
};
6970
},
7071
components: {
@@ -79,34 +80,53 @@ export default {
7980
},
8081
watch: {
8182
"$route.path"() {
83+
this.clearScroll();
8284
this.initConfig();
85+
this.initScroll();
8386
this.changeTabBackground();
8487
}
8588
},
8689
methods: {
90+
clearScroll() {
91+
let {observer} = this;
92+
if(observer) {
93+
document.querySelectorAll('h2[id]').forEach((section) => {
94+
observer.unobserve(section);
95+
});
96+
document.querySelectorAll('h3[id]').forEach((section) => {
97+
observer.unobserve(section);
98+
});
99+
observer.disconnect();
100+
}
101+
},
87102
initScroll() {
103+
const vm = this;
88104
/**
89105
* 目标元素和视窗viewport的交集的变化的方法API
90106
* @type {IntersectionObserver}
91107
*/
92-
const observer = new IntersectionObserver((entries) => {
108+
this.observer = new IntersectionObserver((entries) => {
93109
entries.forEach((entry) => {
94110
let id = entry.target.getAttribute('id');
95111
id = encodeURI(id);
96112
//intersectionRatio:目标元素出现在视窗的比例
97113
if (entry.intersectionRatio > 0) {
98-
document.querySelector(`li a[href="#${id}"]`).parentElement.classList.add('active');
114+
if (document.querySelector(`li a[href="#${id}"]`)) {
115+
document.querySelector(`li a[href="#${id}"]`).parentElement.classList.add('active');
116+
}
99117
} else {
100-
document.querySelector(`li a[href="#${id}"]`).parentElement.classList.remove('active');
118+
if (document.querySelector(`li a[href="#${id}"]`)) {
119+
document.querySelector(`li a[href="#${id}"]`).parentElement.classList.remove('active');
120+
}
101121
}
102122
});
103123
});
104124
//observer.observe():监听当前所有的h2[id]
105125
document.querySelectorAll('h2[id]').forEach((section) => {
106-
observer.observe(section);
126+
vm.observer.observe(section);
107127
});
108128
document.querySelectorAll('h3[id]').forEach((section) => {
109-
observer.observe(section);
129+
vm.observer.observe(section);
110130
});
111131
},
112132
initConfig() {

0 commit comments

Comments
 (0)