@@ -11,6 +11,8 @@ class ScrollspyNav extends Component {
1111 this . headerBackground = this . props . headerBackground === "true" ? true : false ;
1212 this . offset = this . props . offset || 0 ;
1313
14+ this . onScroll = this . onScroll . bind ( this ) ;
15+
1416 if ( this . props . router && this . props . router === "HashRouter" ) {
1517 this . homeDefaultLink = "#/" ;
1618 this . hashIdentifier = "#/#" ;
@@ -20,6 +22,25 @@ class ScrollspyNav extends Component {
2022 }
2123 }
2224
25+ onScroll ( ) {
26+ let scrollSectionOffsetTop ;
27+ this . scrollTargetIds . map ( ( sectionID , index ) => {
28+ scrollSectionOffsetTop = document . getElementById ( sectionID ) . offsetTop - ( this . headerBackground ? document . querySelector ( "div[data-nav='list']" ) . scrollHeight : 0 ) ;
29+
30+ if ( window . pageYOffset >= scrollSectionOffsetTop && window . pageYOffset < scrollSectionOffsetTop + document . getElementById ( sectionID ) . scrollHeight ) {
31+ this . getNavLinkElement ( sectionID ) . classList . add ( this . activeNavClass ) ;
32+ this . clearOtherNavLinkActiveStyle ( sectionID )
33+ } else {
34+ this . getNavLinkElement ( sectionID ) . classList . remove ( this . activeNavClass ) ;
35+ }
36+
37+ if ( window . innerHeight + window . pageYOffset >= document . body . scrollHeight && index === this . scrollTargetIds . length - 1 ) {
38+ this . getNavLinkElement ( sectionID ) . classList . add ( this . activeNavClass ) ;
39+ this . clearOtherNavLinkActiveStyle ( sectionID ) ;
40+ }
41+ } ) ;
42+ }
43+
2344 easeInOutQuad ( current_time , start , change , duration ) {
2445 current_time /= duration / 2 ;
2546 if ( current_time < 1 ) return change / 2 * current_time * current_time + start ;
@@ -79,8 +100,8 @@ class ScrollspyNav extends Component {
79100 }
80101
81102 componentDidMount ( ) {
82- if ( document . querySelector ( `a[href='${ this . homeDefaultLink } ']` ) ) {
83- document . querySelector ( `a[href='${ this . homeDefaultLink } ']` ) . addEventListener ( "click" , ( event ) => {
103+ if ( document . querySelector ( `a[href='${ this . homeDefaultLink } # ']` ) ) {
104+ document . querySelector ( `a[href='${ this . homeDefaultLink } # ']` ) . addEventListener ( "click" , ( event ) => {
84105 event . preventDefault ( ) ;
85106 this . scrollTo ( window . pageYOffset , 0 , this . scrollDuration ) ;
86107 window . location . hash = "" ;
@@ -101,24 +122,11 @@ class ScrollspyNav extends Component {
101122 } ) ;
102123 } )
103124
104- window . addEventListener ( "scroll" , ( ) => {
105- let scrollSectionOffsetTop ;
106- this . scrollTargetIds . map ( ( sectionID , index ) => {
107- scrollSectionOffsetTop = document . getElementById ( sectionID ) . offsetTop - ( this . headerBackground ? document . querySelector ( "div[data-nav='list']" ) . scrollHeight : 0 ) ;
108-
109- if ( window . pageYOffset >= scrollSectionOffsetTop && window . pageYOffset < scrollSectionOffsetTop + document . getElementById ( sectionID ) . scrollHeight ) {
110- this . getNavLinkElement ( sectionID ) . classList . add ( this . activeNavClass ) ;
111- this . clearOtherNavLinkActiveStyle ( sectionID )
112- } else {
113- this . getNavLinkElement ( sectionID ) . classList . remove ( this . activeNavClass ) ;
114- }
125+ window . addEventListener ( "scroll" , this . onScroll ) ;
126+ }
115127
116- if ( window . innerHeight + window . pageYOffset >= document . body . scrollHeight && index === this . scrollTargetIds . length - 1 ) {
117- this . getNavLinkElement ( sectionID ) . classList . add ( this . activeNavClass ) ;
118- this . clearOtherNavLinkActiveStyle ( sectionID ) ;
119- }
120- } ) ;
121- } ) ;
128+ componentWillUnmount ( ) {
129+ window . removeEventListener ( "scroll" , this . onScroll ) ;
122130 }
123131
124132 render ( ) {
0 commit comments