2013-08-01 54 views
0

我正在创建一个单页设计。现在我使用固定的标题菜单以及链接到那里的链接。我已经得到一些代码,以获得顺利的外观。偏移顶部如何实现这个?

问题 当我点击一个菜单项时,它将转到正确的部分,但该部分将位于浏览器屏幕的顶部。现在我想添加一个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; 
    }); 
}); 

谢谢。 卡斯帕

回答

1

尝试:

'scrollTop': $target.offset().top + 120 
+0

谢谢。它正在工作 – Caspert

+0

好的算术! – Jackson