mirror of
https://git.srhtnin.garden/sarah/srhtnin.github.io.git
synced 2026-10-13 18:38:11 +02:00
94 lines
1.6 KiB
JavaScript
94 lines
1.6 KiB
JavaScript
function StartSubNavbar() {
|
|
const SubNavbar =
|
|
document.querySelector(
|
|
".SubNavbar"
|
|
);
|
|
|
|
if (SubNavbar === null) {
|
|
return;
|
|
}
|
|
|
|
const Links = [
|
|
...SubNavbar.querySelectorAll(
|
|
".SubNavbarLink"
|
|
)
|
|
];
|
|
|
|
const AnchorLinks =
|
|
Links.filter(
|
|
Link =>
|
|
Link.getAttribute(
|
|
"href"
|
|
)?.startsWith("#")
|
|
);
|
|
|
|
|
|
function SetCurrentLink(
|
|
CurrentLink
|
|
) {
|
|
for (
|
|
const Link
|
|
of AnchorLinks
|
|
) {
|
|
Link.removeAttribute(
|
|
"aria-current"
|
|
);
|
|
}
|
|
|
|
if (CurrentLink !== null) {
|
|
CurrentLink.setAttribute(
|
|
"aria-current",
|
|
"location"
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
function UpdateFromHash() {
|
|
if (location.hash === "") {
|
|
return;
|
|
}
|
|
|
|
const CurrentLink =
|
|
AnchorLinks.find(
|
|
Link =>
|
|
Link.getAttribute(
|
|
"href"
|
|
) === location.hash
|
|
) ?? null;
|
|
|
|
SetCurrentLink(
|
|
CurrentLink
|
|
);
|
|
}
|
|
|
|
|
|
for (
|
|
const Link
|
|
of AnchorLinks
|
|
) {
|
|
Link.addEventListener(
|
|
"click",
|
|
() => {
|
|
SetCurrentLink(
|
|
Link
|
|
);
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
window.addEventListener(
|
|
"hashchange",
|
|
UpdateFromHash
|
|
);
|
|
|
|
UpdateFromHash();
|
|
}
|
|
|
|
|
|
document.addEventListener(
|
|
"DOMContentLoaded",
|
|
StartSubNavbar
|
|
);
|