javascriptjqueryhtmlcsspage-jump

jQuery - Changing width makes the page jump to the top


I am trying to have this navigation sidebar that slides away some time after the mouse leaves, making the text-part expand. The thing is, that whenever the sliding function starts, the page jumps to the top. There's no "#" used, I tried overflow-y: scroll, return false at the end of the function, preventDefault, but nothing works.

Here's the js code

$(document).ready(function(){
    $("#navbar").delay(5000).animate({left: "-=15%"});
    $("#navwrap").css("width","2%").css("position","fixed");
    $("#bodywrap").delay(5000).animate({width: "90%"});

    $("#navbar").mouseleave(function(){
        $(this).stop(true, true).delay(3000).animate({left: "-=15%"});
        $("#bodywrap").delay(3000).animate({width: "90%"});
        $("#navwrap").css("width","2%").css("position","fixed");
    });

    $("#navwrap").mouseenter(function(){
        $("#navbar").stop(true,false);
        $("#bodywrap").stop(true,false);
        if ($("#navbar").css("left") != "0%"){
            $("#navbar").animate({left: "0%"});
            $(this).css("width","15%").css("position","initial");
            $("#bodywrap").animate({width: "75%"});
        };
    });
});

And here's the fiddle: http://jsfiddle.net/uuw2dzry/1/


Solution

  • Much of the animations and delays can be done by CSS

    http://jsfiddle.net/fkL57v9d/

    Your code is overly complicated and leaves room for bugs and undesired results.
    I'd consider re-creating this page in a simpler way.