@@ -2,19 +2,16 @@ function forEach(elems, fun) {
22 Array . prototype . forEach . call ( elems , fun ) ;
33}
44
5+ function getPagetocElems ( ) {
6+ return document . getElementsByClassName ( "pagetoc" ) [ 0 ] . children ;
7+ }
8+
59// Un-active everything when you click it
610function forPagetocElem ( fun ) {
7- forEach ( document . getElementsByClassName ( "pagetoc" ) [ 0 ] . children , fun ) ;
11+ forEach ( getPagetocElems ( ) , fun ) ;
812}
913
10- forPagetocElem ( function ( el ) {
11- el . addEventHandler ( "click" , function ( ) {
12- forPagetocElem ( function ( el ) {
13- el . classList . remove ( "active" ) ;
14- } ) ;
15- el . classList . add ( "active" ) ;
16- } ) ;
17- } ) ;
14+ var activeHref = location . href ;
1815
1916function getRect ( element ) {
2017 return element . getBoundingClientRect ( ) ;
@@ -30,11 +27,23 @@ function overflowBottom(container, element) {
3027
3128var updateFunction = function ( ) {
3229 var id = undefined ;
30+
31+ if ( location . href != activeHref ) {
32+ activeHref = location . href ;
33+ forPagetocElem ( function ( el ) {
34+ if ( el . href == activeHref ) {
35+ id = el ;
36+ }
37+ } ) ;
38+ }
39+
3340 var elements = document . getElementsByClassName ( "header" ) ;
41+ let menuBottom = getRect ( document . getElementById ( "menu-bar" ) ) . bottom ;
3442 let contentCenter = window . innerHeight / 2 ;
3543 let margin = contentCenter / 3 ;
44+
3645 forEach ( elements , function ( el , i , arr ) {
37- if ( id === undefined && getRect ( el ) . top >= contentCenter - margin ) {
46+ if ( id === undefined && getRect ( el ) . bottom >= menuBottom ) {
3847 if ( getRect ( el ) . top >= contentCenter + margin ) {
3948 id = arr [ Math . max ( 0 , i - 1 ) ] ;
4049 } else {
@@ -48,6 +57,7 @@ var updateFunction = function () {
4857 } ) ;
4958
5059 if ( ! id ) return ;
60+
5161 forPagetocElem ( function ( el ) {
5262 if ( id . href . localeCompare ( el . href ) == 0 ) {
5363 el . classList . add ( "active" ) ;
0 commit comments