December 01, 2021
Previously examples of this that I’ve found and used don’t always seem to work. I found an updated version that works well.
(function(jQuery, window) {
var adjustAnchor = function() {
var jQueryanchor = jQuery(':target'),
fixedElementHeight = 170;
if (jQueryanchor.length > 0) {
jQuery('html, body')
.stop()
.animate({
scrollTop: jQueryanchor.offset().top - fixedElementHeight
}, 200);
}
};
jQuery(window).on('hashchange load', function() {
adjustAnchor();
});
})(jQuery, window);