@@ -51,13 +51,32 @@ export default {
5151 },
5252
5353 mounted () {
54- this .initItems ()
55- this .removeActiveClass ()
56- this .currentItem = this .getItemInsideWindow ()
54+ this .$nextTick (() => {
55+ this .initItems ()
56+
57+ const hash = window .location .hash
58+
59+ if (hash) {
60+ const isAnchor = hash .indexOf (' -' ) > - 1
61+ const activeItem = isAnchor
62+ ? this .$el .querySelector (` a[href='${ hash .substr (0 , hash .indexOf (' -' ))} ']` )
63+ : this .$el .querySelector (` a[href='${ hash} ']` )
64+ const currentSection = document .getElementById (hash .substr (1 ))
5765
58- if (this .currentItem ) this .currentItem .classList .add (this .activeClass )
66+ this .updateActiveItem (activeItem)
67+
68+ currentSection .scrollIntoView ({
69+ behavior: ' instant' ,
70+ block: ' start' ,
71+ inline: ' nearest'
72+ })
73+ }
5974
60- this .scrollContainer .addEventListener (' scroll' , this .onScroll )
75+ // Scrolling lacks callback & is async
76+ setTimeout (() => {
77+ this .scrollContainer .addEventListener (' scroll' , this .onScroll )
78+ }, 100 )
79+ })
6180 },
6281
6382 updated () {
@@ -70,24 +89,15 @@ export default {
7089
7190 methods: {
7291 onScroll (event ) {
92+ console .log (' scroll' )
7393 this .currentItem = this .getItemInsideWindow ()
7494
7595 if (this .currentItem && this .currentItem !== this .lastActiveItem ) {
7696 const { hash } = this .currentItem
7797
78- this .removeActiveClass ()
7998 this .lastActiveItem = this .currentItem
8099
81- this .currentItem .classList .add (this .activeClass )
82-
83- const currentParent = this .currentItem .closest (this .itemClass + ' s' )
84- .previousElementSibling .classList
85-
86- if (currentParent .contains (this .itemClass .substr (1 ))) {
87- currentParent .add (this .activeParentClass )
88- }
89-
90- this .currentItem .scrollIntoView ({ block: ' end' , inline: ' nearest' })
100+ this .updateActiveItem (this .currentItem )
91101
92102 this .updateHash (hash)
93103 }
@@ -123,31 +133,17 @@ export default {
123133 handleClick (event ) {
124134 event .preventDefault ()
125135
126- const isAnchor = ! event .currentTarget .classList .contains (this .itemClass .substr (1 ))
127136 const target = event .currentTarget
128137 const hash = target .hash
138+ const isAnchor = hash .indexOf (' -' ) > - 1
129139 const item = isAnchor
130- ? document .querySelector (` a[href='${ hash .substr (0 , hash .indexOf (' -' ))} ']` )
140+ ? this . $el .querySelector (` a[href='${ hash .substr (0 , hash .indexOf (' -' ))} ']` )
131141 : target
132142 const section = document .getElementById (hash .substr (1 ))
133143
134144 this .scrollContainer .removeEventListener (' scroll' , this .onScroll )
135145
136- this .removeActiveClass ()
137-
138- item .classList .add (this .activeClass )
139-
140- const parent = item .closest (this .itemClass + ' s' ).previousElementSibling .classList
141-
142- if (parent .contains (this .itemClass .substr (1 ))) {
143- parent .add (this .activeParentClass )
144- }
145-
146- item .scrollIntoView ({
147- behavior: ' instant' ,
148- block: ' end' ,
149- inline: ' nearest'
150- })
146+ this .updateActiveItem (item)
151147
152148 section .scrollIntoView ({
153149 behavior: ' instant' ,
@@ -183,6 +179,20 @@ export default {
183179 }
184180 },
185181
182+ updateActiveItem (item ) {
183+ this .removeActiveClass ()
184+
185+ item .classList .add (this .activeClass )
186+
187+ const parent = item .closest (this .itemClass + ' s' ).previousElementSibling .classList
188+
189+ if (parent .contains (this .itemClass .substr (1 ))) {
190+ parent .add (this .activeParentClass )
191+ }
192+
193+ item .scrollIntoView ({ block: ' end' , inline: ' nearest' })
194+ },
195+
186196 removeActiveClass () {
187197 this .items .forEach (item => {
188198 item .classList .remove (this .activeClass , this .activeParentClass )
0 commit comments