Offset scroll anchor in HTML with Bootstrap 4 fixed navbar

前端 未结 5 593
孤独总比滥情好
孤独总比滥情好 2020-12-29 06:17

So, I have this single page that consists of a few sections. Users can go to these sections by scrolling themselves or clicking in the navbar (a href with anchor). Due to th

5条回答
  •  猫巷女王i
    2020-12-29 07:13

    you can use jQuery to override the default behavior so you don't have to change the layout ( margin, padding .. etc.)

      var divId;
    
      $('.nav-link').click(function(){    
        divId = $(this).attr('href');
         $('html, body').animate({
          scrollTop: $(divId).offset().top - 54
        }, 100);
      });
    

    https://codepen.io/anon/pen/NYRvaL

提交回复
热议问题