simple scrolling script

simple scrolling script

Hi everyone

Im new to this forum, so thanks in advance to anyone who can point me in the right direction!

I have a script which I modified to suit my needs, which works, except it's showing some weird results.

Allow me to explain...
  1. $(function(){
        $('a').click(function(){
            var targetID = $(this).attr('href');
            var targetPos = $(targetID).offset().top
            $('html, body').animate({scrollTop: targetPos-15}, 800);
            return false;
        });
    });






The script is smooth scrolling me to a div whose id equals the anchor.

ie

  1. <a href="#hello">hello</a>
scrolls down to

  1. <div id="hello">
  2. content
  3. </div>
This all appeared to be working fine, but I started noticing that some of the links were scrolling me to the top of the page instead.

These links are all in unordered lists, and I have realised that it is always the sixth list item which does this in any ul...

  1. <ul>
  2.       <li><a href="#one">this works</a></li>
  3.       <li><a href="#two">this works</a></li>
  4.       <li><a href="#three">this works</a></li>
  5.       <li><a href="#four">this works</a></li>
  6.       <li><a href="#five">this works</a></li>
  7.       <li><a href="#six">scrolls to the top of the page</a></li>
  8.       <li><a href="#seven">this works</a></li>
  9.       ..etc...
  10. </ul>
I have checked and rechecked all of my code to make sure im not just making stupid mistakes, and I am pretty sure that it's the script...

Any ideas on this would be so welcome!

Many many thanks to anyone reading...

EDIT:

The above was using jQuery 1.3.2

After testing with 1.4.2 I found that I still have a problem, except the link no longer scrolls me to the top of the page, it just does nothing!

I wonder if I could alter the script somehow to work around this if it is indeed a bug?

Thanks!