mirror of
https://git.srhtnin.garden/sarah/srhtnin.github.io.git
synced 2026-10-14 02:38:12 +02:00
Added subnavbar.
This commit is contained in:
1 parent
bdf6871b84
commit
46c35689d7
8 files changed
+298
-19
No files matched your search
@@ -0,0 +1,93 @@
|
||||
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
|
||||
);
|
||||
Reference in new issue
Block a user