@@ -23,6 +23,13 @@ export default {
23
23
document . body . scrollTop
24
24
)
25
25
26
+ const scrollHeight = Math . max (
27
+ document . documentElement . scrollHeight ,
28
+ document . body . scrollHeight
29
+ )
30
+
31
+ const bottomY = window . innerHeight + scrollTop
32
+
26
33
for ( let i = 0 ; i < anchors . length ; i ++ ) {
27
34
const anchor = anchors [ i ]
28
35
const nextAnchor = anchors [ i + 1 ]
@@ -31,9 +38,19 @@ export default {
31
38
|| ( scrollTop >= anchor . parentElement . offsetTop + 10
32
39
&& ( ! nextAnchor || scrollTop < nextAnchor . parentElement . offsetTop - 10 ) )
33
40
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
+ }
35
52
this . $vuepress . $set ( 'disableScrollBehavior' , true )
36
- this . $router . replace ( decodeURIComponent ( anchor . hash ) , ( ) => {
53
+ this . $router . replace ( decodeURIComponent ( activeAnchor . hash ) , ( ) => {
37
54
// execute after scrollBehavior handler.
38
55
this . $nextTick ( ( ) => {
39
56
this . $vuepress . $set ( 'disableScrollBehavior' , false )
0 commit comments