Refactored all functionality into one function. Changed margin-top.

This commit is contained in:
Abijeet 2017-11-05 20:23:16 +05:30
parent 1031c61d0c
commit bdba25b6f2
1 changed files with 47 additions and 43 deletions

View File

@ -156,6 +156,12 @@ let setupPageShow = window.setupPageShow = function (pageId) {
if ('IntersectionObserver' in window &&
'IntersectionObserverEntry' in window &&
'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
addPageHighlighting();
}
function addPageHighlighting() {
let $pageNav = null;
$(document).ready(function () {
// fetch all the headings.
let headings = document.querySelector('.page-content').querySelectorAll('h1, h2, h3, h4, h5, h6');
@ -164,15 +170,13 @@ let setupPageShow = window.setupPageShow = function (pageId) {
addNavObserver(headings);
}
});
}
let $pageNav = null;
function addNavObserver(headings) {
// Setup the intersection observer.
// margin top = -25px to trigger the threshold change before the heading
// margin top = -35px to trigger the threshold change before the heading
// has completely left the viewport on the top.
let intersectOpts = {
rootMargin: '-25px 0px 0px 0px',
rootMargin: '-35px 0px 0px 0px',
threshold: 1.0
}
$pageNav = $('.sidebar-page-nav');
@ -202,7 +206,7 @@ let setupPageShow = window.setupPageShow = function (pageId) {
$pageNav.find('a').removeClass('current-heading');
$pageNav.find('a[href="#' + elementId + '"]').addClass('current-heading');
}
}
};
module.exports = setupPageShow;