babbaggeii
babbaggeii

Reputation: 7737

jquery how to offset scroll position

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

Answers (3)

ShibinRagh
ShibinRagh

Reputation: 6646

Try

$('html, body').animate({
    scrollTop: $("#maincontent").offset().top - 100 // Means Less header height
},400);

Upvotes: 8

David Fregoli
David Fregoli

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

user2188149
user2188149

Reputation:

$("#maincontent").scrollTop(300);

Upvotes: 0

Related Questions