【问题标题】:Anchor position is equal to position minus header height锚点位置等于位置减去标题高度
【发布时间】:2016-11-04 23:16:34
【问题描述】:

我有固定的标题。当我点击锚链接时,它会在那个位置上。问题是我的固定标题的高度与该位置重叠。我想当我去某个锚点时,它的位置是 = 锚点 - 标题高度。

这是我的 javascript 代码:

<script> <!--Anchors-->
$(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
        }, 1000);
        return false;
      }
    }
  });
});
</script>

【问题讨论】:

  • 能把你的html和css代码放上来
  • 没有必要。 &lt;a href="#somediv"&gt; Link &lt;/a&gt; &lt;div id="somediv"&gt; ... content &lt;/div&gt;

标签: javascript jquery css html


【解决方案1】:

您可以在页面内容之前添加一些边距

#fixed-header{
    position: fixed;
    height: 100px;
    top: 0;
}
body { 
    margin-top: 100px;
}

【讨论】:

  • 对我没有帮助,因为我的标题不在页面顶部。有width:100vwheight:100vh 的第一个主页部分,然后是我的粘性标题。
  • @pHenomen 这就是为什么您需要为所有问题添加更多代码的原因。怎么会有人知道呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 2012-04-29
  • 2014-03-17
相关资源
最近更新 更多