Created
July 27, 2023 17:37
-
-
Save VincentVToscano/dfb7242473ffe116bbaf9f9fcf31fe54 to your computer and use it in GitHub Desktop.
Scroll to target (element) with an adjustment or offset
This file contains hidden or 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 scrollToTargetAdjusted(){ | |
var element = document.getElementById('targetElement'); | |
var headerOffset = 45; | |
var elementPosition = element.getBoundingClientRect().top; | |
var offsetPosition = elementPosition + window.pageYOffset - headerOffset; | |
window.scrollTo({ | |
top: offsetPosition, | |
behavior: "smooth" | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment