I've a strange behavor using scrollTop JQuery function. I've a div that contains a complex text divided in different sections like this:
<div id="wrapper" ... >
<div id="section1">
<h1>Section 1</h1>
Lorem ipsum dolor sit amet, consectetuer...
...
</div>
<div id="section2">
<h1>Section 2</h1>
Lorem ipsum dolor sit amet, consectetuer...
...
</div>
<div id="section3">
<h1>Section 3</h1>
Lorem ipsum dolor sit amet, consectetuer...
...
</div>
...
a second DIV contains a simple list of sections and when user click on each of them I want wrapper scroll at the right section:
<li>
<a href="#" onclick="customScrollTo('section1')">section1</a>
</li>
<li>
<a href="#" onclick="customScrollTo('section2')">section2</a>
...
and this is simple the JS function:
function customScrollTo (section) {
$('#wrapper').animate({
scrollTop: $("#wrapper div[id='" + section + "']").offset().top
}, 200);
};
now if you try to test it in this JSfiddle: http://jsfiddle.net/Ws5F9/4/
you can see two strange behavors:
- if you click 'section 1' than 'section 2' than 'section 3', section 3 doesn't work!
- if you click twice on the same section link, the second time it scroll at page top