Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何向jquery scrolltop添加偏移量_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

Javascript 如何向jquery scrolltop添加偏移量

Javascript 如何向jquery scrolltop添加偏移量,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter 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

我正在使用以下代码对引导上导航栏中的菜单链接的滚动设置动画:

$("#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
,以便为导航栏留出空间。我通过动态获取
#subnavbar
高度()
实现了这一点,因此,如果将来需要更改它的高度,您无需担心破坏此代码

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