Offset Anchor for Fixed Header

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);

Source.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.