Is there a way to margin an anchor in css?

前端 未结 5 1113
你的背包
你的背包 2021-02-12 12:18

I have a fixed header height 50px. In my body, I have a lot of anchors. The problem is that, when I click on links pointing to anchors, the anchor appears under my fixed header

5条回答
  •  暖寄归人
    2021-02-12 12:29

    If you use jQuery, you use this function:

    function scrollToAnchor() {
        if($(".jquery-anchor").length > 0 && document.URL.indexOf("#") >= 0){
        var anchor = document.URL.split("#")[1];
        $(".jquery-anchor").each(function() {
            if($(this).attr("name") == anchor) {
                $("html,body").animate({
                        scrollTop: $(this).offset().top - 50},
                    'slow');
                }
            });
        }
    }
    

    then add the class "jquery_anchor" to your anchor

提交回复
热议问题