0

我正在创建一个单页设计。现在我使用带有链接到那里部分的链接的固定标题菜单。我得到了一些代码来获得平滑的外观。

问题 当我单击一个菜单项时,它将转到正确的部分,但该部分将位于浏览器屏幕的顶部。现在我想添加一个 120px 顶部的偏移量。我怎样才能做到这一点?

代码:

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

谢谢你。卡斯帕

4

1 回答 1

1

尝试:

'scrollTop': $target.offset().top + 120
于 2013-08-01T19:18:53.243 回答