Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Offsetting scrollTop

I have the following snippet

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

On click of a link I would like the browser to display from near the top of the #menu div. I would like it display just few pixels lines before the menu.

How can I achieve this?

I have added paddingTop:5 to offset() but this is not the desired result.

like image 670
InvalidSyntax Avatar asked May 12 '26 00:05

InvalidSyntax


1 Answers

Just subtract whatever amount you want from $('#menu').offset().top:

$('html,body').animate({
    scrollTop: $('#menu').offset().top - 5 // or 10
}, 'slow');

Here's the fiddle: http://jsfiddle.net/qVWuv/

like image 94
Joseph Silber Avatar answered May 14 '26 13:05

Joseph Silber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!