jQuery scrollTo goess beyond target section

jQuery scrollTo goess beyond target section

I am using the jQuery scrollTo function to scroll to sections on my website. However, the first click after the page loads, always scrolls beyond the target section but subsequent clicks behaves normal. Any ideas how I can fix this. My code looks like below.

  1.   $("body").on('click', "#links li",  function () {
  2.      
  3.     var scrollHash = $(this).attr("class");
  4.     $('html, body').animate({
  5.       scrollTop: $("#" + scrollHash + " " + "h2").offset().top - 10,
  6.     }, 1000);
  7.     window.location.hash = scrollHash;
  8.   });