2014-08-31 62 views
0

我目前使用此函數在使用Bootstrap的同一頁面中將光滑(https://github.com/flesler/jquery.scrollTo)滾動到目標散列。在目標元素上方的固定像素處停止jquery滾動

$('.navbar .destScroll').bind('click', 'ul li a', function(event) { 
    $.scrollTo(event.target.hash, 500); 
    event.preventDefault(); 
}); 

由於沒有在頁面頂部的固定80px菜單,我想滾動停止80px目標哈希以上。

請幫忙。

+0

簡單:嘗試:'$( 「正文」)動畫({scrollTop的: 「」 + $(event.target).offset( ).top +「」},500);' – 2014-08-31 12:32:16

回答

0

您可以嘗試使用我的scrollTo代碼,這是偉大的 - https://gist.github.com/yairEO/7030050

,你可以使用它像這樣:

// there are 3 parameters: 
// jumpIn - in which container to scroll (default body/html) 
// quick - jump with timed animation or not (default with) 
// offset - destination offset from target 
$('#there').scrollToPos(null, null, -200); 

在上面的例子中,該窗口將滾動到#there的位置元素,頂部偏移量爲-200


另一種直接綁定導致另一位置的點擊的方法是使用鏈接h作爲 data-scrollTo屬性,它是一個選擇器,一個元件跳到:

<a data-scrollTo='#there'>go there</a> 
+1

感謝vsync。這是我想要的。無論如何,在我的情況下,因爲有很多目標可以滾動到,我將你的代碼修改爲$(event.target.hash).scrollToPos(null,null,-80);它完美的作品。再次感謝! – 2014-08-31 13:00:11