Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

smooth scroll to target with offset

function scrollToTargetAdjusted(){
    var element = document.getElementById('targetElement');
    var headerOffset = 45;
    var elementPosition = element.getBoundingClientRect().top;
    var offsetPosition = elementPosition - headerOffset;

    window.scrollTo({
         top: offsetPosition,
         behavior: "smooth"
    });
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #smooth #scroll #target #offset
ADD COMMENT
Topic
Name
9+5 =