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...
- $(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
- <a href="#hello">hello</a>
scrolls down to
- <div id="hello">
- content
- </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...
- <ul>
- <li><a href="#one">this works</a></li>
- <li><a href="#two">this works</a></li>
- <li><a href="#three">this works</a></li>
- <li><a href="#four">this works</a></li>
- <li><a href="#five">this works</a></li>
- <li><a href="#six">scrolls to the top of the page</a></li>
- <li><a href="#seven">this works</a></li>
- ..etc...
- </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!