File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22 const progressBar = document . getElementById ( "scrollProgressBar" ) ;
33 if ( ! progressBar ) return ;
44
5- const updateProgress = ( ) => {
6- const scrollTop = window . scrollY ;
7- const scrollHeight =
8- document . documentElement . scrollHeight - window . innerHeight ;
9-
10- const progress =
11- scrollHeight > 0 ? ( scrollTop / scrollHeight ) * 100 : 0 ;
5+ let ticking = false ;
126
7+ const updateProgress = ( ) => {
8+ const scrollTop = globalThis . scrollY ;
9+ const scrollHeight = document . documentElement . scrollHeight - globalThis . innerHeight ;
10+ const progress = scrollHeight > 0 ? ( scrollTop / scrollHeight ) * 100 : 0 ;
1311 progressBar . style . width = `${ progress } %` ;
12+ ticking = false ;
13+ } ;
14+
15+ const onScroll = ( ) => {
16+ if ( ! ticking ) {
17+ requestAnimationFrame ( updateProgress ) ;
18+ ticking = true ;
19+ }
1420 } ;
1521
16- window . addEventListener ( "scroll" , updateProgress , { passive : true } ) ;
17- window . addEventListener ( "resize" , updateProgress ) ;
22+ globalThis . addEventListener ( "scroll" , onScroll , { passive : true } ) ;
23+ globalThis . addEventListener ( "resize" , onScroll ) ;
1824
1925 if ( "ResizeObserver" in window ) {
20- new ResizeObserver ( updateProgress ) . observe ( document . body ) ;
26+ new ResizeObserver ( onScroll ) . observe ( document . body ) ;
2127 }
2228
2329 updateProgress ( ) ;
24- } ) ( ) ;
30+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments