Run ScrollTop with offset of element by ID

Staffan Estberg picture Staffan Estberg · Mar 8, 2012 · Viewed 151.9k times · Source

Trying to make the browser scroll to a specific ID with an added offset -

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

What I need to do is to set the offset by -100px. How can I accomplish this?

Answer

charlietfl picture charlietfl · Mar 8, 2012

No magic involved, just subtract from the offset top of the element

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