繁体   English   中英

如何通过导航栏的高度更改平滑滚动JQuery动画的偏移量

[英]How to change offset on smooth scroll JQuery animation by a height of the navbar

我所有的平滑滚动都可以完美地进行,但是,锚点停在页面的顶部,但是导航栏固定在顶部,因此我需要做的是将导航栏的顶部偏移顶部。 我将如何处理?

<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>

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

暂无
暂无

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

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