1- import { getTargetElement , getTransitionDelayDurationFromElement } from "../../modules/utility.js"
1+ import { getTargetElement , getTransitionDelayDurationFromElement } from "../../modules/utility.js"
22import Data from "../../modules/data.js"
33
44export function init ( id ) {
@@ -48,7 +48,7 @@ export function init(id) {
4848 }
4949 Data . set ( id , menu )
5050
51- scrollElementToView ( menu . element )
51+ scrollElementToView ( menu )
5252}
5353
5454export function update ( id ) {
@@ -74,19 +74,24 @@ export function update(id) {
7474 }
7575 } ) ;
7676
77- scrollElementToView ( menu . element )
77+ scrollElementToView ( menu )
7878}
7979
8080export function scrollToView ( id ) {
8181 const menu = Data . get ( id )
8282 if ( menu ) {
83- scrollElementToView ( menu . element ) ;
83+ scrollElementToView ( menu ) ;
8484 }
8585}
8686
8787export function dispose ( id ) {
8888 const menu = Data . get ( id )
89- Data . remove ( id )
89+ Data . remove ( id ) ;
90+
91+ if ( menu . handler ) {
92+ clearInterval ( menu . handler ) ;
93+ menu . handler = null ;
94+ }
9095
9196 menu . collapses . forEach ( el => {
9297 const target = getTargetElement ( el )
@@ -97,13 +102,23 @@ export function dispose(id) {
97102 } )
98103}
99104
100- const scrollElementToView = element => {
105+ const scrollElementToView = menu => {
106+ const { element } = menu ;
101107 const scroll = element . hasAttribute ( 'data-bb-scroll-view' )
102108 if ( scroll ) {
103- var links = [ ...element . querySelectorAll ( '.nav-link.active' ) ]
104- if ( links . length > 0 ) {
105- var link = links . pop ( )
106- link . scrollIntoView ( { behavior : 'smooth' , block : 'center' , inline : 'start' } ) ;
107- }
109+ let count = 0 ;
110+ menu . handler = setInterval ( ( ) => {
111+ if ( count < 3 ) {
112+ var links = [ ...element . querySelectorAll ( '.nav-link.active' ) ]
113+ if ( links . length > 0 ) {
114+ clearInterval ( menu . handler ) ;
115+ menu . handler = null ;
116+
117+ var link = links . pop ( )
118+ link . scrollIntoView ( { behavior : 'smooth' , block : 'center' , inline : 'start' } ) ;
119+ }
120+ }
121+ count ++ ;
122+ } , 100 ) ;
108123 }
109124}
0 commit comments