如何将平滑滚动 JQuery 动画上的偏移量更改为导航栏的高度



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

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

最新更新