Last active
February 10, 2024 15:48
-
-
Save benhatsor/01cd928520e2f4348c95c2d5cf3cc30f to your computer and use it in GitHub Desktop.
Smooth scroll to element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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