2017-06-12 88 views

回答

0

您應該調整此值scrollTop: $(hash).offset().top。如custom.js中的scrollTop: $(hash).offset().top+50;scrollTop: $(hash).offset().top-50;

$(document).ready(function(){ 
     // Add smooth scrolling to all links 
     $(".hashtag 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-100 

      // scrollTop: $(hash).offset().top+50; 
      }, 1000, function(){ 

      // Add hash (#) to URL when done scrolling (default click behavior) 
      window.location.hash = hash; 
      }); 
     } // End if 
     }); 
    }); 
+0

如果我將這個「scrollTop:$(hash).offset()。top + 50;」添加到我的代碼中,那麼它不會像以前一樣順利滾動到該位置。 – Priya

+0

在這裏除去';'分號'scrollTop:$(hash).offset()。top + 50;'。它應該是'scrollTop:$(hash).offset()。top + 50' – vel

+0

set'scrollTop:$(hash).offset()。top-100;' – vel

0

在頁腳中添加此腳本以添加頂部偏移量。

<script type="text/javascript"> 
$(document).ready(function(){ 
    // Add smooth scrolling to all links 
    $(".hashtag 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 

     var target_offset = $(hash).offset() ? $(hash).offset().top : 0; 
     //change this number to create the additional off set   
     var customoffset = 40 
     $('html, body').animate({scrollTop:target_offset - customoffset}, 1000, function(){ 

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