繁体   English   中英

如何向jquery scrolltop添加偏移量

[英]How to add offset to jquery scrolltop

我正在使用以下代码在bootstrap上的导航栏中的菜单链接中滚动滚动:

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

您需要从目标元素的offset().top减去60 ,以允许导航栏的空间。 我通过获取#subnavbarheight()来动态完成这项#subnavbar这样你以后需要更改它的高度,你不必担心破坏这段代码。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM