简体   繁体   English

滚动到锚点上方100px

[英]Scroll to 100px above the anchor

I'm using the the JavaScript code below to create a scroll effect from my nav to the anchor. 我正在使用下面的JavaScript代码创建从导航到锚点的滚动效果。

The problem I am having is I want the scrolling to stop 100px above the anchor. 我遇到的问题是我希望滚动停止在锚点上方100px。

What would I need to change in this code to achieve this result? 我需要在此代码中更改哪些内容才能实现此结果?

$(document).ready(function() {
  $('a[href^="#"]').click(function() {
      var target = $(this.hash);
      if (target.length == 0) target = $('a[name="' + this.hash.substr(1) + '"]');
      if (target.length == 0) target = $('html');
      $('html, body').animate({ scrollTop: target.offset().top }, 1000);
      return false;
  });
});

Thank you 谢谢

Subtract 100 pixels from target.offset().top. 从target.offset()。顶部减去100个像素。 like so: 像这样:

$(document).ready(function() {
  $('a[href^="#"]').click(function() {
      var target = $(this.hash);
      if (target.length == 0) target = $('a[name="' + this.hash.substr(1) + '"]');
      if (target.length == 0) target = $('html');
      $('html, body').animate({ scrollTop: target.offset().top-100 }, 1000);
      return false;
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM