Skip to content

Commit e52e52f

Browse files
author
Joseph Lowinske
committed
implement fix for flicker occurring on long TOCs when scrolling up from page bottom
1 parent 7d2881f commit e52e52f

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

js/customscripts.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ $(function() {
2828
$mobile_menu = $('nav.mobile_expanded'),
2929
$colSidebar = $('.col-sidebar'),
3030
$sidebar = $('#mysidebar'),
31-
$tocRight = $('#toc-right'),
3231
$footer = $('section.footer'),
3332
sideNavHeight = ($('.nav--home').length > 0) ? '40px' : '60px';
34-
$versionSwitcher = $('#version-switcher');
33+
$versionSwitcher = $('#version-switcher'),
34+
$tocRight = $('#toc-right');
3535

3636
function collapseSideNav() {
3737
$('.collapsed-header').fadeIn(250);
@@ -108,12 +108,27 @@ $(function() {
108108
cachedWidth = _viewport_width;
109109
});
110110

111+
var tocHeight = 0; // outer var for TOC height reference maintained outside scroll handler
112+
111113
$(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+
112127
var scrollTop = $(window).scrollTop();
113128
var windowHeight = $(window).height();
114129
var footerOffset = $footer.offset().top;
115130
var viewportFooterDiff = (scrollTop + windowHeight) - footerOffset - 1;
116-
var tocHeightInColumn = $tocRight.height() + parseInt($tocRight.css('top'));
131+
var tocHeightInColumn = tocHeight + parseInt($tocRight.css('top')),
117132
_viewport_width = window.innerWidth;
118133

119134
$sidebar.css('padding-top', '');
@@ -149,7 +164,7 @@ $(function() {
149164
width: '265px'
150165
});
151166

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
153168
// otherwise, unset bottom property
154169
if (scrollTop + tocHeightInColumn >= footerOffset) {
155170
$tocRight.css('bottom', viewportFooterDiff + 1 + 'px');

0 commit comments

Comments
 (0)