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);
});