Right-nav highlighting and auto-scroll

This commit is contained in:
John Mulhausen 2017-03-01 15:21:37 -08:00
parent 6173c8a65d
commit af58236190
No known key found for this signature in database
GPG Key ID: 0FC599ECCDBFDE02
1 changed files with 68 additions and 1 deletions

View File

@ -1,3 +1,71 @@
// Right nav highlighting
function highlightRightNav(heading)
{
if (heading == "title")
{
history.pushState({},"Top of page on " + document.location.pathname,document.location.protocol +"//"+ document.location.hostname + (location.port ? ':'+location.port: '') + document.location.pathname);
$("#my_toc a").each(function(){
$(this).removeClass("active");
});
$("#sidebar-wrapper").animate({
scrollTop: 0
},800);
} else {
var targetAnchorHREF = document.location.protocol +"//"+ document.location.hostname + (location.port ? ':'+location.port: '') + document.location.pathname + "#" + heading;
// make sure we aren't filtering out that heading level
var noFilterFound = false;
$("#my_toc a").each(function(){
if (this.href==targetAnchorHREF) {
noFilterFound = true;
}
});
// now, highlight that heading
if (noFilterFound)
{
$("#my_toc a").each(function(){
//console.log("right-nav",this.href);
if (this.href==targetAnchorHREF)
{
history.pushState({},this.innerText,targetAnchorHREF);
$(this).addClass("active");
$("#sidebar-wrapper").animate({
scrollTop: $("#sidebar-wrapper").scrollTop() + $(this).position().top - 200
},10);
//document.getElementById("sidebar-wrapper").scrollTop = this.getBoundingClientRect().top - 200;
} else {
$(this).removeClass("active");
}
});
}
}
}
var currentHeading = "";
$(window).scroll(function(){
var headingPositions = new Array();
$("h1, h2, h3, h4, h5, h6").each(function(){
if (this.id == "") this.id="title";
headingPositions[this.id]=this.getBoundingClientRect().top;
});
headingPositions.sort();
// the headings have all been grabbed and sorted in order of their scroll
// position (from the top of the page). First one is toppermost.
for(var key in headingPositions)
{
if (headingPositions[key] > 0 && headingPositions[key] < 200)
{
if (currentHeading != key)
{
// a new heading has scrolled to within 200px of the top of the page.
// highlight the right-nav entry and de-highlight the others.
highlightRightNav(key);
currentHeading = key;
}
break;
}
}
});
// Cookie functions
function createCookie(name,value,days) {
var expires = "";
@ -192,4 +260,3 @@ if($('.nav-sidebar ul a.active').length != 0)
$(this).addClass('collapse in').siblings;
});
}