Created
February 26, 2018 21:19
-
-
Save ozcan/d4c9aae1721471ee198cbc542983e067 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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