1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00
Radicale/assets/navigation.js

66 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2022-07-28 16:20:55 +02:00
window.addEventListener("DOMContentLoaded", function() {
function findActiveSection(sections) {
let result = sections[0];
for (let [section, link] of sections) {
if (section.getBoundingClientRect().y > 10) {
break;
}
result = [section, link];
}
return result;
}
let nav = document.querySelector("nav");
2020-02-27 14:18:57 +01:00
let sections = [[document.querySelector("main"), null]];
for (let section of document.querySelectorAll("section")) {
let id = section.getAttribute("id");
let link = nav.querySelector("a[href=\\#" + id.replace(/\//g, "\\/") + "]");
2020-03-23 20:49:08 +01:00
if (link) {
link = link.parentElement;
2020-02-27 14:18:57 +01:00
link.classList.remove("active");
sections.push([section, link]);
}
}
let oldLink = null;
function updateLink() {
let [section, link] = findActiveSection(sections);
while (oldLink) {
if (oldLink.tagName === "LI") {
oldLink.classList.remove("active");
if (!oldLink.classList.contains("level4")) {
break;
}
}
oldLink = oldLink.parentElement;
}
oldLink = link;
while (link) {
if (link.tagName === "LI") {
link.classList.add("active");
if (!link.classList.contains("level4")) {
break;
}
}
link = link.parentElement;
}
2020-03-23 20:49:08 +01:00
if (link) {
let topLink = link.getBoundingClientRect().y;
let topNav = nav.getBoundingClientRect().y;
2020-03-23 20:49:08 +01:00
nav.scrollTop += topLink - topNav - 10;
} else {
nav.scrollTop = 0;
}
}
function resizeNav() {
let height = window.innerHeight - nav.getBoundingClientRect().y;
nav.style.maxHeight = height > 0 ? height + "px" : "none";
}
function updateNav() {
resizeNav();
updateLink();
}
document.addEventListener("scroll", updateNav);
window.addEventListener("resize", updateNav);
updateNav();
});