From a8c4f887e5fe4426df825aa39648a3fff2231416 Mon Sep 17 00:00:00 2001 From: George Cushen Date: Thu, 11 Jul 2019 20:54:52 +0100 Subject: [PATCH] fix: navbar active link highlighting and scrolling Fixes some edge cases caused due to responsive navbar height and dynamic Portfolio widget height, as well as case where offset was non-integer, requiring float to be rounded up. Fix #1199 --- assets/js/academic.js | 31 +++++++++++++++++++++---------- layouts/_default/baseof.html | 2 +- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/assets/js/academic.js b/assets/js/academic.js index b0d01c1f..69d21367 100644 --- a/assets/js/academic.js +++ b/assets/js/academic.js @@ -11,9 +11,13 @@ * Responsive scrolling for URL hashes. * --------------------------------------------------------------------------- */ - // Dynamically get responsive navigation bar offset. - let $navbar = $('.navbar'); - let navbar_offset = $navbar.innerHeight(); + // Dynamically get responsive navigation bar height for offsetting Scrollspy. + function getNavBarHeight() { + let $navbar = $('.navbar'); + let navbar_offset = $navbar.innerHeight(); + console.debug('Navbar height: ' + navbar_offset); + return navbar_offset; + } /** * Responsive hash scrolling. @@ -30,9 +34,10 @@ // If target element exists, scroll to it taking into account fixed navigation bar offset. if($(target).length) { + let elementOffset = Math.ceil($(target).offset().top - getNavBarHeight()); // Round up to highlight right ID! $('body').addClass('scrolling'); $('html, body').animate({ - scrollTop: $(target).offset().top - navbar_offset + scrollTop: elementOffset }, 600, function () { $('body').removeClass('scrolling'); }); @@ -46,7 +51,7 @@ let $body = $('body'); let data = $body.data('bs.scrollspy'); if (data) { - data._config.offset = navbar_offset; + data._config.offset = getNavBarHeight(); $body.data('bs.scrollspy', data); $body.scrollspy('refresh'); } @@ -77,8 +82,15 @@ // Use jQuery's animate() method for smooth page scrolling. // The numerical parameter specifies the time (ms) taken to scroll to the specified hash. + let elementOffset = Math.ceil($(hash).offset().top - getNavBarHeight()); // Round up to highlight right ID! + + // Uncomment to debug. + // let scrollTop = $(window).scrollTop(); + // let scrollDelta = (elementOffset - scrollTop); + // console.debug('Scroll Delta: ' + scrollDelta); + $('html, body').animate({ - scrollTop: $(hash).offset().top - navbar_offset + scrollTop: elementOffset }, 800); } }); @@ -446,6 +458,9 @@ * --------------------------------------------------------------------------- */ $(window).on('load', function() { + // Re-initialize Scrollspy with dynamic navbar height offset. + fixScrollspy(); + if (window.location.hash) { // When accessing homepage from another page and `#top` hash is set, show top of page (no hash). if (window.location.hash == "#top") { @@ -459,10 +474,6 @@ } } - // Initialize Scrollspy. - let $body = $('body'); - $body.scrollspy({offset: navbar_offset }); - // Call `fixScrollspy` when window is resized. let resizeTimer; $(window).resize(function() { diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 107ee5d7..49332629 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -3,7 +3,7 @@ {{ partial "site_head.html" . }} - + {{ partial "search" . }}