Skip to content

Instantly share code, notes, and snippets.

@benhatsor
Last active February 10, 2024 15:48
Show Gist options
  • Save benhatsor/01cd928520e2f4348c95c2d5cf3cc30f to your computer and use it in GitHub Desktop.
Save benhatsor/01cd928520e2f4348c95c2d5cf3cc30f to your computer and use it in GitHub Desktop.
Smooth scroll to element
function scrollToEl(el) {
var rect = el.getBoundingClientRect(),
scrollTop = window.pageYOffset || document.documentElement.scrollTop,
scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
elTop = rect.top + scrollTop,
elLeft = rect.left + scrollLeft;
window.scrollTo({
top: elTop,
left: elLeft,
behavior: 'smooth'
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment