2013-08-01 90 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