我正在创建一个单页设计。现在我使用一个固定的标题菜单,其中包含链接到那些部分的链接。我已经获得了一些代码以获得平滑的外观。
问题 当我单击一个菜单项时,它将转到正确的部分,但该部分将位于浏览器屏幕的顶部。现在我想添加一个120px顶部的偏移量。我怎么能这样做?
CODE:
// When you click on an <a> that has a '#'
$('nav#primary-navwrapper a[href^="#"]').bind('click.smoothscroll',function (e) {
// Prevent from default action to intitiate
e.preventDefault();
// Targets the part of the address that comes after the '#'
var target = this.hash;
$target = $(target);
$('html, body').stop().animate({
// The .offset() method allows us to retrieve the current position of an element relative to the document.
// Here we are using it to find the position of the target, which we defined earlier as the section of the address that will come after the '#'
'scrollTop': $target.offset().top
}, 500, 'swing', function () {
window.location.hash = target;
});
});
谢谢。 卡斯帕
答案 0 :(得分:1)
尝试:
'scrollTop': $target.offset().top + 120