链接到顶部div

问题描述:

我有一个完整的引导程序布局,固定导航栏高度为140px,每个div部分都通过导航链接。当我点击链接转到about部分时,它似乎直接转到h2标签,而不是about section div的开头。有没有办法可以使用js来修复截断问题?链接到顶部div

<ul> 
<li><a href>#about</a></li> 
</ul> 

部分{填充:5em的0 5em的0}

<section id="about"> 
    <div class="container"> 
    <div class="text-center"> 
    <h2>About</h2> 
    <h3>About Subheading</h3> 
     <div class="about"> 
      <div class="row"> 
       <div class="col-lg-12"> 
        </div> 
       </div> 

      </div> 
     </div> 
    </div> 
    </div> 
    </div> 
</section> 

刚才试了我自己,我无法重现您的问题:

http://jsfiddle.net/DTcHh/27912/

你有许多您发布的HTML中的错误,例如缺少href属性和额外的</div>结束标记

+0

我有导航栏静态顶采用的140px 20像素填充顶部和底部的高度。点击链接时是否需要一些偏移量,以便它进入

的开始> – 7O07Y7

+0

我已经更新了小提琴,似乎仍然跳到了节 – ioseph

试试这个

编辑的代码视图

$(function() { 
    $('a[href*="#"]:not([href="#"])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
     if (target.length) { 
     $('html, body').animate({ 
      scrollTop: target.offset().top - 140 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
});