stylescape
Version:
Stylescape is a visual identity framework developed by Scape Agency.
28 lines • 1.22 kB
JavaScript
export class SoftScrollManager {
static enableForSelector(selector, yOffsetInRem = 0) {
const links = document.querySelectorAll(selector);
const offsetPx = yOffsetInRem *
parseFloat(getComputedStyle(document.documentElement).fontSize);
links.forEach((link) => {
if (!(link instanceof HTMLAnchorElement))
return;
const href = link.getAttribute("href");
if (!href || !href.startsWith("#") || href === "#")
return;
const targetId = href.slice(1);
link.addEventListener("click", (event) => {
event.preventDefault();
const targetElement = document.getElementById(targetId);
if (!targetElement) {
console.warn(`SoftScroll: Element not found for ID "${targetId}"`);
return;
}
const scrollTargetY = targetElement.getBoundingClientRect().top +
window.pageYOffset -
offsetPx;
window.scrollTo({ top: scrollTargetY, behavior: "smooth" });
});
});
}
}
//# sourceMappingURL=SoftScrollManager.js.map