Skip to content

Commit 521dddd

Browse files
KyleJShaverulivz
authored andcommitted
fix($theme-default): nav url change bug (close: #865) (#1475)
1 parent 7fc9047 commit 521dddd

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

packages/@vuepress/plugin-active-header-links/clientRootMixin.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ export default {
2323
document.body.scrollTop
2424
)
2525

26+
const scrollHeight = Math.max(
27+
document.documentElement.scrollHeight,
28+
document.body.scrollHeight
29+
)
30+
31+
const bottomY = window.innerHeight + scrollTop
32+
2633
for (let i = 0; i < anchors.length; i++) {
2734
const anchor = anchors[i]
2835
const nextAnchor = anchors[i + 1]
@@ -31,9 +38,19 @@ export default {
3138
|| (scrollTop >= anchor.parentElement.offsetTop + 10
3239
&& (!nextAnchor || scrollTop < nextAnchor.parentElement.offsetTop - 10))
3340

34-
if (isActive && decodeURIComponent(this.$route.hash) !== decodeURIComponent(anchor.hash)) {
41+
const routeHash = decodeURIComponent(this.$route.hash)
42+
if (isActive && routeHash !== decodeURIComponent(anchor.hash)) {
43+
let activeAnchor = anchor
44+
// check if anchor is at the bottom of the page to keep $route.hash consistent
45+
if (bottomY === scrollHeight) {
46+
for (let j = i + 1; j < anchors.length; j++) {
47+
if (routeHash === decodeURIComponent(anchors[j].hash)) {
48+
activeAnchor = anchors[j]
49+
}
50+
}
51+
}
3552
this.$vuepress.$set('disableScrollBehavior', true)
36-
this.$router.replace(decodeURIComponent(anchor.hash), () => {
53+
this.$router.replace(decodeURIComponent(activeAnchor.hash), () => {
3754
// execute after scrollBehavior handler.
3855
this.$nextTick(() => {
3956
this.$vuepress.$set('disableScrollBehavior', false)

0 commit comments

Comments
 (0)