@@ -28,10 +28,10 @@ $(function() {
28
28
$mobile_menu = $ ( 'nav.mobile_expanded' ) ,
29
29
$colSidebar = $ ( '.col-sidebar' ) ,
30
30
$sidebar = $ ( '#mysidebar' ) ,
31
- $tocRight = $ ( '#toc-right' ) ,
32
31
$footer = $ ( 'section.footer' ) ,
33
32
sideNavHeight = ( $ ( '.nav--home' ) . length > 0 ) ? '40px' : '60px' ;
34
- $versionSwitcher = $ ( '#version-switcher' ) ;
33
+ $versionSwitcher = $ ( '#version-switcher' ) ,
34
+ $tocRight = $ ( '#toc-right' ) ;
35
35
36
36
function collapseSideNav ( ) {
37
37
$ ( '.collapsed-header' ) . fadeIn ( 250 ) ;
@@ -108,12 +108,27 @@ $(function() {
108
108
cachedWidth = _viewport_width ;
109
109
} ) ;
110
110
111
+ var tocHeight = 0 ; // outer var for TOC height reference maintained outside scroll handler
112
+
111
113
$ ( window ) . on ( 'scroll' , function ( e ) {
114
+ // If we calculate tocHeight inside of scroll handler, the true TOC height will be
115
+ // miscalculated as too small when a long TOC exceeds the top border of the footer.
116
+ // This will cause a long TOC to flicker when the user scrolls up.
117
+ //
118
+ // To solve this, we need to calculate the TOC height outside the event handler--
119
+ // however, the TOC is rendered *after* the 'ready' event on $(document) is fired, thus we cannot
120
+ // simply calculate the TOC height at the top of the 'ready' handler. The `if` block below this is a hack
121
+ // to get the 'true' height of the TOC once it has been rendered on the page.
122
+ var tempTocHeight = $tocRight . height ( )
123
+ if ( tempTocHeight > tocHeight ) {
124
+ tocHeight = tempTocHeight ;
125
+ }
126
+
112
127
var scrollTop = $ ( window ) . scrollTop ( ) ;
113
128
var windowHeight = $ ( window ) . height ( ) ;
114
129
var footerOffset = $footer . offset ( ) . top ;
115
130
var viewportFooterDiff = ( scrollTop + windowHeight ) - footerOffset - 1 ;
116
- var tocHeightInColumn = $tocRight . height ( ) + parseInt ( $tocRight . css ( 'top' ) ) ;
131
+ var tocHeightInColumn = tocHeight + parseInt ( $tocRight . css ( 'top' ) ) ,
117
132
_viewport_width = window . innerWidth ;
118
133
119
134
$sidebar . css ( 'padding-top' , '' ) ;
@@ -149,7 +164,7 @@ $(function() {
149
164
width : '265px'
150
165
} ) ;
151
166
152
- // if footer in view and TOC overruns footer, set bottom property to top of footer
167
+ // if footer in view and TOC overruns top of footer, set bottom property to top of footer
153
168
// otherwise, unset bottom property
154
169
if ( scrollTop + tocHeightInColumn >= footerOffset ) {
155
170
$tocRight . css ( 'bottom' , viewportFooterDiff + 1 + 'px' ) ;
0 commit comments