2019-09-11 06:19:34 +02:00
|
|
|
<style type="text/css" media="screen">
|
|
|
|
.post-menu ul {
|
|
|
|
list-style: none;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<div class="post-menu">
|
2020-07-12 10:19:42 +02:00
|
|
|
<div class="post-menu-title">TOC</div>
|
2019-09-11 06:19:34 +02:00
|
|
|
<div class="post-menu-content"></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script>
|
2019-09-24 08:10:57 +02:00
|
|
|
function generateContent() {
|
2019-12-05 12:07:34 +01:00
|
|
|
var menu = document.querySelector(".post-menu");
|
|
|
|
var menuContent = menu.querySelector(".post-menu-content");
|
2019-09-24 08:10:57 +02:00
|
|
|
var headings = document.querySelector(".post-content").querySelectorAll("h2, h3, h4, h5, h6");
|
2019-09-11 06:19:34 +02:00
|
|
|
|
2019-12-05 12:07:34 +01:00
|
|
|
// Hide menu when no headings
|
|
|
|
if (headings.length === 0) {
|
|
|
|
return menu.style.display = "none";
|
|
|
|
}
|
|
|
|
|
2019-09-24 08:10:57 +02:00
|
|
|
// Generate post menu
|
|
|
|
var menuHTML = '';
|
|
|
|
for (var i = 0; i < headings.length; i++) {
|
|
|
|
var h = headings[i];
|
|
|
|
menuHTML += (
|
|
|
|
'<li class="h-' + h.tagName.toLowerCase() + '">'
|
|
|
|
+ '<a href="#h-' + h.getAttribute('id') + '">' + h.textContent + '</a></li>');
|
|
|
|
}
|
2019-09-11 06:19:34 +02:00
|
|
|
|
2019-12-05 12:07:34 +01:00
|
|
|
menuContent.innerHTML = '<ul>' + menuHTML + '</ul>';
|
2019-09-11 06:19:34 +02:00
|
|
|
|
2019-09-24 08:10:57 +02:00
|
|
|
// The header element
|
|
|
|
var header = document.querySelector('header.site-header');
|
2019-09-11 06:19:34 +02:00
|
|
|
|
2020-10-03 06:23:29 +02:00
|
|
|
function doMenuCollapse(index, over_items) {
|
2020-08-31 11:21:45 +02:00
|
|
|
var items = menuContent.firstChild.children;
|
2020-08-31 13:46:13 +02:00
|
|
|
|
2020-10-03 06:23:29 +02:00
|
|
|
if (over_items == undefined) {
|
|
|
|
over_items = 20;
|
|
|
|
}
|
|
|
|
|
2020-08-31 13:46:13 +02:00
|
|
|
if (items.length < over_items) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-31 11:21:45 +02:00
|
|
|
var activeItem = items[index];
|
|
|
|
var beginItem = activeItem
|
|
|
|
var endItem = activeItem
|
|
|
|
var beginIndex = index;
|
|
|
|
var endIndex = index + 1;
|
|
|
|
while (beginIndex >= 0
|
|
|
|
&& !items[beginIndex].classList.contains('h-h2')) {
|
|
|
|
beginIndex -= 1;
|
|
|
|
}
|
|
|
|
while (endIndex < items.length
|
|
|
|
&& !items[endIndex].classList.contains('h-h2')) {
|
|
|
|
endIndex += 1;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < beginIndex; i++) {
|
|
|
|
item = items[i]
|
|
|
|
if (!item.classList.contains('h-h2')) {
|
|
|
|
item.style.display = 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var i = beginIndex + 1; i < endIndex; i++) {
|
|
|
|
item = items[i]
|
|
|
|
// if (!item.classList.contains('h-h2')) {
|
|
|
|
item.style.display = '';
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
for (var i = endIndex; i < items.length; i++) {
|
|
|
|
item = items[i]
|
|
|
|
if (!item.classList.contains('h-h2')) {
|
|
|
|
item.style.display = 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init menu collapsed
|
|
|
|
doMenuCollapse(-1);
|
|
|
|
|
2019-09-24 08:10:57 +02:00
|
|
|
// Active the menu item
|
|
|
|
window.addEventListener('scroll', function (event) {
|
2019-12-05 12:07:34 +01:00
|
|
|
var lastActive = menuContent.querySelector('.active');
|
2019-09-24 08:10:57 +02:00
|
|
|
var changed = true;
|
2020-08-31 11:21:45 +02:00
|
|
|
var activeIndex = -1;
|
2019-09-24 08:10:57 +02:00
|
|
|
for (var i = headings.length - 1; i >= 0; i--) {
|
|
|
|
var h = headings[i];
|
2019-09-24 09:19:05 +02:00
|
|
|
var headingRect = h.getBoundingClientRect();
|
|
|
|
var headerRect = header.getBoundingClientRect();
|
|
|
|
var headerTop = Math.floor(headerRect.top);
|
|
|
|
var headerHeight = Math.floor(headerRect.height);
|
|
|
|
var headerHeight = headerTop + headerHeight + 20;
|
|
|
|
if (headingRect.top <= headerHeight) {
|
2019-09-24 08:10:57 +02:00
|
|
|
var id = 'h-' + h.getAttribute('id');
|
2020-07-12 10:19:42 +02:00
|
|
|
var a = menuContent.querySelector('a[href="#' + id + '"]');
|
|
|
|
var curActive = a.parentNode;
|
2019-09-24 08:10:57 +02:00
|
|
|
if (curActive) {
|
|
|
|
curActive.classList.add('active');
|
2020-08-31 11:21:45 +02:00
|
|
|
activeIndex = i;
|
2019-09-24 08:10:57 +02:00
|
|
|
}
|
|
|
|
if (lastActive == curActive) {
|
|
|
|
changed = false;
|
|
|
|
}
|
|
|
|
break;
|
2019-09-11 06:19:34 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-31 11:21:45 +02:00
|
|
|
if (changed) {
|
|
|
|
if (lastActive) {
|
|
|
|
lastActive.classList.remove('active');
|
|
|
|
}
|
|
|
|
doMenuCollapse(activeIndex);
|
2019-09-24 08:10:57 +02:00
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
generateContent();
|
2019-09-11 06:19:34 +02:00
|
|
|
</script>
|