关于javascript:偏移HTML锚以调整为固定标头

offsetting an html anchor to adjust for fixed header

本问题已经有最佳答案,请猛点这里访问。

我正在使用Twitter Bootstrap构建网站,并且在页面顶部具有40px固定位置的导航栏。 我设置了一些锚点以跳转到整个页面上的各个H1,但是锚点目标最终被导航栏部分掩盖了。 有没有办法抵消导航栏帐户的链接? 我已经看到一个建议将每个锚点放在自己的偏移div中,但这似乎是很多重复的代码,我想知道是否还有另一种方法。

如果可能的话,我宁愿只使用CSS而不是jQuery来做到这一点,但是任何解决方案都是可以的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.topbar {
   height: 40px;
   position: fixed;
   top: 0;
   left: 0;
   right: 0;
   z-index: 10000;
   overflow: visible;
}

body {
  padding-top: 40px;
     /* 40px to make the container go all the way to the bottom of the topbar */
     font-family: 'Proxima',"Helvetica Neue", Helvetica, Arial, sans-serif;
     font-size: 14px;
     line-height: 18px;
      font-weight: normal;
      color: #555;
      background-color: #eee;
 }

谢谢!