Skip to content

Instantly share code, notes, and snippets.

@ozcan
Created February 26, 2018 21:19
Show Gist options
  • Save ozcan/d4c9aae1721471ee198cbc542983e067 to your computer and use it in GitHub Desktop.
Save ozcan/d4c9aae1721471ee198cbc542983e067 to your computer and use it in GitHub Desktop.
// Scrolling changes background-color of li element in table-of-contents
$(document).ready(function() {
var screenHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
window.addEventListener('scroll', function(event) {
if (!$('#table-of-contents').length)
return;
var min_offset = null;
var target_id = null;
$(':header').each(function (index, item) {
if ($(item).attr('id')) {
var offset = $(item).offset().top - $(document).scrollTop();
if (offset < (screenHeight / 3)) {
if (min_offset == null || Math.abs(offset) < Math.abs(min_offset)) {
min_offset = offset;
target_id = $(item).attr('id');
}
}
};
});
$("#markdown-toc a").removeClass('header-active');
if (target_id) {
$("#markdown-toc a[href$='#"+target_id+"']").addClass('header-active');
}
}, true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment