Reputation: 7737
I have the following code that scrolls to a certain element:
$('html, body').animate({
scrollTop: $("#maincontent").offset().top
}, 400);
However, I have a fixed navbar at the top and so would like to be able to offset this. How can I offset it by a number of pixels?
Upvotes: 1
Views: 28289
Reputation: 6646
Try
$('html, body').animate({
scrollTop: $("#maincontent").offset().top - 100 // Means Less header height
},400);
Upvotes: 8
Reputation: 3407
$("#maincontent").offset().top
just returns an integer, you only need to add or subtract to it to change the offset
$("#maincontent").offset().top - 100
Upvotes: 1