首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何按导航栏的高度更改平滑滚动JQuery动画中的偏移

问如何按导航栏的高度更改平滑滚动JQuery动画中的偏移
EN

Stack Overflow用户
提问于 2017-08-15 06:16:19
回答 1查看 488关注 0票数 1

我所有的平滑滚动都能完美地工作,然而,锚点停在页面的最顶部,但我的导航栏固定在顶部,所以我需要它做的是将顶部偏移导航栏的高度。我该怎么做呢?

代码语言:javascript
复制
<script>
  $(document).ready(function(){
    // Add smooth scrolling to all links
    $("a").on('click', function(event) {

      // Make sure this.hash has a value before overriding default behavior
      if (this.hash !== "") {
        // Prevent default anchor click behavior
        event.preventDefault();

        // Store hash
        var hash = this.hash;

        // Using jQuery's animate() method to add smooth page scroll
        // The optional number (800) specifies the number of milliseconds
        // it takes to scroll to the specified area
        $('html, body').animate({
          scrollTop: $(hash).offset().top
        }, 1000, function(){

          // Add hash (#) to URL when done scrolling (default click behavior)
          window.location.hash = hash;
        });
      } // End if
    });
  });
</script>
EN

回答 1

Stack Overflow用户

发布于 2017-08-15 06:19:42

非常简单:scrollTop: $('#hash').offset().top - navbarHeight。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45683828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档