Fixes horizontal scroll behavior for <nav> on small screens
This commit is contained in:
parent
a5e9d7298a
commit
8c58a5a697
@ -88,9 +88,7 @@ document.addEventListener("DOMContentLoaded", () =>
|
|||||||
);
|
);
|
||||||
|
|
||||||
const scrollActiveItemIntoView = (link, behavior = "auto") => {
|
const scrollActiveItemIntoView = (link, behavior = "auto") => {
|
||||||
const item = link?.closest("li");
|
if (!link) {
|
||||||
|
|
||||||
if (!item) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,8 +98,10 @@ document.addEventListener("DOMContentLoaded", () =>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const itemLeft = item.offsetLeft;
|
const listRect = list.getBoundingClientRect();
|
||||||
const itemCenter = itemLeft + (item.offsetWidth / 2);
|
const linkRect = link.getBoundingClientRect();
|
||||||
|
const itemLeft = linkRect.left - listRect.left + list.scrollLeft;
|
||||||
|
const itemCenter = itemLeft + (linkRect.width / 2);
|
||||||
const maxScrollLeft = list.scrollWidth - list.clientWidth;
|
const maxScrollLeft = list.scrollWidth - list.clientWidth;
|
||||||
const targetLeft = Math.max(0, Math.min(maxScrollLeft, itemCenter - (list.clientWidth / 2)));
|
const targetLeft = Math.max(0, Math.min(maxScrollLeft, itemCenter - (list.clientWidth / 2)));
|
||||||
|
|
||||||
@ -140,6 +140,9 @@ document.addEventListener("DOMContentLoaded", () =>
|
|||||||
};
|
};
|
||||||
|
|
||||||
updateActive();
|
updateActive();
|
||||||
|
links.forEach((link) => {
|
||||||
|
link.addEventListener("click", () => scrollActiveItemIntoView(link, "smooth"));
|
||||||
|
});
|
||||||
window.addEventListener("scroll", () => updateActive("smooth"), { passive: true });
|
window.addEventListener("scroll", () => updateActive("smooth"), { passive: true });
|
||||||
window.addEventListener("resize", updateActive);
|
window.addEventListener("resize", updateActive);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user