2012-03-08 63 views
43

試圖讓瀏覽器滾動到一個特定的ID與加上偏移量偏移元素 -運行scrollTop的由ID

$('html, body').animate({scrollTop: $('#contact').offset().top}, 'slow'); 

我需要做的是通過-100px設定偏移。我怎樣才能做到這一點?

回答

101

沒有魔法參與,單從元件

$('html, body').animate({scrollTop: $('#contact').offset().top -100 }, 'slow'); 
+13

神的偏移最高減,它是那麼容易......我覺得自己像一個10歲。 – 2012-03-08 14:40:13

+1

使用window.innerHeight確定用戶的屏幕高度。 – 2013-08-15 16:07:53

+0

@Phillip在這種情況下屏幕高度與任何事情有什麼關係? – charlietfl 2013-08-15 16:31:09