3

我正在使用以下代码从引导程序导航栏中的菜单链接动画滚动:

$("#subnavbar ul li a[href^='#']").on('click', function(e) {
   e.preventDefault();
   $('html, body').animate({ scrollTop: $(this.hash).offset().top }, 600);

   // edit: Opera and IE requires the "html" elm. animated
});

目前,固定导航栏将锚点隐藏在下方。如何添加 60px 的偏移量来调整?

4

1 回答 1

8

您需要60offset().top目标元素中减去,以便为导航栏留出空间。我已经通过获取 的 来动态完成此操作height()#subnavbar因此如果您将来需要更改其高度,则无需担心破坏此代码。

$("#subnavbar ul li a[href^='#']").on('click', function(e) {
    e.preventDefault();
    $('html, body').animate({ 
        scrollTop: $(this.hash).offset().top - $('#subnavbar').height()
    }, 600);
});
于 2013-08-17T13:09:13.743 回答