首页 > 解决方案 > JQuery - offset.top - 将 80px 添加到返回值

问题描述

所以我有一个网站,我实现了平滑滚动。当用户单击指向我页面上某个位置的链接时,动画将执行到目标,offset.top我需要将 80px 添加到该值,然后滚动到该元素。这是我现在正在使用的代码:

$(document).ready(function() {
  $("a").on('click', function(event) {
    if (this.hash !== "") {
      event.preventDefault();
      var hash = this.hash;

      $('html, body').animate({
       scrollTop: $(hash).offset().top
      }, 500, function() {

      window.location.hash = hash;
      });
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

标签: javascriptjquery

解决方案


应该很简单

scrollTop: $(hash).offset().top + 80

推荐阅读