File tree 2 files changed +43
-3
lines changed
2 files changed +43
-3
lines changed Original file line number Diff line number Diff line change @@ -2,11 +2,38 @@ export default {
2
2
created ( ) {
3
3
this . $vuepress . $on ( 'AsyncMarkdownContentMounted' , ( ) => {
4
4
this . $vuepress . $set ( 'contentMounted' , true )
5
+
6
+ document . querySelectorAll ( 'a[href^="#"]' ) . forEach ( anchor => {
7
+ anchor . addEventListener ( 'click' , function ( e ) {
8
+ console . log ( e )
9
+ e . preventDefault ( )
10
+ window . scroll ( {
11
+ top : e . target . offsetTop - 75 ,
12
+ left : 0 ,
13
+ behavior : 'smooth'
14
+ } )
15
+ } )
16
+ } )
17
+
18
+ if ( this . $route . hash ) {
19
+ try {
20
+ const anchor = document . getElementById ( this . $route . hash . slice ( 1 ) )
21
+ const anchorLink = anchor . querySelector ( 'a.header-anchor' )
22
+ console . log ( anchorLink . offsetTop - 70 )
23
+ window . scroll ( {
24
+ top : anchorLink . offsetTop - 70 ,
25
+ left : 0 ,
26
+ behavior : 'auto'
27
+ } )
28
+ } catch ( e ) {
29
+ console . error ( e )
30
+ }
31
+ }
5
32
} )
6
33
} ,
7
34
8
35
watch : {
9
- $page ( ) {
36
+ '$route.path' ( ) {
10
37
this . $vuepress . $set ( 'contentMounted' , false )
11
38
}
12
39
}
Original file line number Diff line number Diff line change @@ -41,11 +41,21 @@ function getAnchors () {
41
41
} )
42
42
}
43
43
44
+ let freezeScrollEvent = false
45
+
44
46
export default {
47
+ watch : {
48
+ '$route.path' ( ) {
49
+ console . log ( '$route.path changed' )
50
+ freezeScrollEvent = true
51
+ }
52
+ } ,
53
+
45
54
mounted ( ) {
46
55
this . $vuepress . $on ( 'AsyncMarkdownContentMounted' , ( slotKey ) => {
56
+ freezeScrollEvent = false
47
57
if ( slotKey === 'default' ) {
48
- window . addEventListener ( 'scroll' , this . onScroll )
58
+ window . addEventListener ( 'scroll' , ( ) => this . onScroll ( freezeScrollEvent ) )
49
59
}
50
60
} )
51
61
@@ -66,7 +76,10 @@ export default {
66
76
} ,
67
77
68
78
methods : {
69
- onScroll : throttle ( function ( ) {
79
+ onScroll : throttle ( function ( freezeScrollEvent ) {
80
+ if ( freezeScrollEvent ) {
81
+ return
82
+ }
70
83
const anchors = getAnchors ( )
71
84
if ( anchors . length === 0 ) {
72
85
return
You can’t perform that action at this time.
0 commit comments