TEUCG
8/30/2016 - 10:46 PM

Anchor scroll with fixed header offset

Anchor scroll with fixed header offset

(function($) {
  $(window).load(function() {
    $('a[href*=#]:not([href=#])').click(function() 
    {
      if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        || location.hostname == this.hostname) 
      {
      
        var target = $(this.hash),
        headerHeight = $(".primary-header").height() + 5; // Get fixed header height
            
        target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
              
        if (target.length) 
        {
          $('html,body').animate({
            scrollTop: target.offset().top - headerHeight
          }, 500);
          return false;
        }
      }
    });
  });
})(jQuery);