Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | export function animateScrollTo(to: string | number | HTMLElement, offset = 0) { const element = (document.scrollingElement as HTMLElement) || window; if (typeof to === "string") { to = document.querySelector(to) as HTMLElement; if (!to) { throw Error(`Can't find any element for "${to}" in animateScrollTo.`); } } if (typeof to !== "number") { to = to.getBoundingClientRect().top + element.scrollTop; } to = to - offset; if (element.scrollTo) { element.scrollTo({ top: to, left: 0, behavior: "smooth" }); } else { element.scrollTop = to; } } export function initLinkScroll(link: HTMLLinkElement, { offset = 0 }) { if (link && (link.dataset.scrollTo || link.href)) { const href = (link.dataset.scrollTo as string) || (link.getAttribute("href") as string); const target = document.querySelector(href) as HTMLElement; if (target) { link.addEventListener("click", (event) => { event.preventDefault(); animateScrollTo(target, offset); setTimeout(() => window.history.pushState({}, "", href), 100); }); } } } |