top nav bar blocking top content of the page

前端 未结 19 1633
孤城傲影
孤城傲影 2020-11-28 00:48

I have this Twitter Bootstrap code

  
19条回答
  •  甜味超标
    2020-11-28 01:19

    Two problems will happen here:

    1. Page load (content hidden)
    2. Internal links like this will scroll to the top, and be hidden by the navbar:
                  
    hello  
    

    World

    Bootstrap 4 w/ internal page links

    To fix 1), as Martijn Burger said above, the bootstrap v4 starter template css uses:

    body {
      padding-top: 5rem;
    }
    

    To fix 2) check out this issue. This code mostly works (but not on 2nd click of same hash):

    window.addEventListener("hashchange", function() { scrollBy(0, -70) })
    

    This code animates A links with jQuery (not slim jQuery):

      // inline theme global code here
      $(document).ready(function() {
        var body = $('html,body'), NAVBAR_HEIGHT = 70;
        function smoothScrollingTo(target) {
          if($(target)) body.animate({scrollTop:$(target).offset().top - NAVBAR_HEIGHT}, 500);
        }
        $('a[href*=\\#]').on('click', function(event){
          event.preventDefault();
          smoothScrollingTo(this.hash);
        });
        $(document).ready(function(){
          smoothScrollingTo(location.hash);
        });
      })
    

提交回复
热议问题