0

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?

babbaggeii
  • 7,577
  • 20
  • 64
  • 118

3 Answers3

7

Try

$('html, body').animate({
    scrollTop: $("#maincontent").offset().top - 100 // Means Less header height
},400);
ShibinRagh
  • 6,530
  • 4
  • 35
  • 57
0
$("#maincontent").scrollTop(300);
0

$("#maincontent").offset().top just returns an integer, you only need to add or subtract to it to change the offset

$("#maincontent").offset().top - 100
David Fregoli
  • 3,377
  • 1
  • 19
  • 40