Fixed page header overlaps in-page anchors

前端 未结 30 2807
逝去的感伤
逝去的感伤 2020-11-22 00:25

If I have a non-scrolling header in an HTML page, fixed to the top, having a defined height:

Is there a way to use the URL anchor (the #fragment part) t

30条回答
  •  广开言路
    2020-11-22 00:49

    A minimally intrusive approach using jQuery:

    Link:

    Go To Anchor 1
    

    Content:

    Here is Anchor 1

    Script:

    $(".anchor-link").click(function() {
        var headerHeight = 120;
        $('html, body').stop(true, true).animate({
            scrollTop: $(this.hash).offset().top - headerHeight
        }, 750);
        return false;
    });
    

    By assigning the anchor-link class to the links, the behaviour of other links (like accordion or tab controls) are not affected.

    The question doesn't want javascript but the other more popular question is closed because of this one and I couldn't answer there.

提交回复
热议问题