Just move the elements before the animation: http://jsfiddle.net/YWnzc/12/ .
I used $.doTimeout
and $.scrollTo
for convenience. I also made out the regexp number. Timeout is the ability to move to a div without scrolling backward.
var current, prev; jQuery( "#nav").delegate( "a", "mouseenter mouseleave", function(e){ var i, self = this, pos; if( e.type == "mouseleave" ) { i = 1; } else { i = $(this).attr("href").match(/(\d)$/)[1]; } //stop the previous animation, otherwise it will be queued if(e.type === "mouseleave") { var elem = $("#section1").insertBefore(current); elem = $("#section1"); $.doTimeout("test", 500, function() { current = $("#section1"); jQuery("#wrapper").scrollTo(elem, 250); }); } else { var elem = $("#section" + i); elem.insertAfter(current || "#section1"); current = elem; $.doTimeout("test"); jQuery("#wrapper").scrollTo(elem, 250); } }); jQuery( "#wrapper").on("mouseover", function() { jQuery( "#wrapper").stop(); });
source share