title: JavaScript scrolling unpredictable up and down
body: <p>I've got a big or maybe small problem. I'm trying to scroll my website, so each container slides to a specific position to the top of the website. But, as far as i can see, there is a problem, because if you click the first item and then the next, the script scrolls to the same position, but if you click on a item above the previous clicked, it scrolls to the bottom of the page.</p>
  
  <p>my script for this is a simple anchorscript, take a look:</p>
  
  <pre><code>$("a.scrollForMe").each(function() {   // go through all links
  var href = $(this).attr("href");
  if (typeof(href) == "undefined") { return; }
  if ($(this).attr("href").match(/^#/)) {   // if href starts with #
      $(this).click(function() {  // add a click event to it
          var name = $(this).attr("href").substring(1);  // get the anchor link
  
          // on click, scroll to the a with a name attribute matching the anchor
          $('html, body').animate({scrollTop: $("section[name='" + name + "']").offset().top - 420}, 1000);
  
          //alert ("This height is: " + $("section[name='" + name + "']").height() + " and it's name is " + name);
      });
  }
  });
  </code></pre>
  
  <p>If you want to see, how its working at the moment, take a look at my website:
  <a href="http://1st-issue.de/2012/redaxo/#sec10" rel="nofollow">http://1st-issue.de/2012/redaxo/#sec10</a></p>
  
  <p>i hope you can help me! Thanks in advance!</p>