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