【问题标题】:How to add offset to jquery scrolltop如何向jquery scrolltop添加偏移量
【发布时间】:2013-08-17 13:06:54
【问题描述】:

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

$("#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 的偏移量来调整?

【问题讨论】:

    标签: javascript jquery twitter-bootstrap


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 2011-07-25
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多