function scrollTo(target, offsetPx = 0) {
const el = typeof target === 'string' ? document.querySelector(target) : target;
if (!el) return;
const top = el.getBoundingClientRect().top + window.scrollY - offsetPx;
window.scrollTo({ top, behavior: 'smooth' });
}
// Usage — scroll to section, accounting for a 64px fixed nav
scrollTo('#contact', 64);
// Or smooth-scroll all anchor links
document.querySelectorAll('a[href^="#"]').forEach((a) => {
a.addEventListener('click', (e) => {
e.preventDefault();
scrollTo(a.getAttribute('href'), 64);
});
});
Create a free account and build your private vault. Share publicly whenever you want.