Skip to content

Instantly share code, notes, and snippets.

@VincentVToscano
Created July 27, 2023 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VincentVToscano/dfb7242473ffe116bbaf9f9fcf31fe54 to your computer and use it in GitHub Desktop.
Save VincentVToscano/dfb7242473ffe116bbaf9f9fcf31fe54 to your computer and use it in GitHub Desktop.
Scroll to target (element) with an adjustment or offset
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