I wrote the following to create an animation which collapses a navigation bar, load new content for it and expands it again. Meanwhile, the rest of the page content fades out, loads and fades in again.
It works and looks good, and is even timed well. But my code is one hell of a mess. How could I have done better?
$(function() {
var newHash = "",
$NavmainContent = $("#contentNavigationWrapper"),
$mainContent = $(".contentWrapper"),
$first = $('#currentSection'),
$second = $('#project-tools'),
$third = $('#project-pagetools'),
$fourth = $('#cp'),
$pageWrap = $("#page-wrap"),
baseHeight = 0,
$el;
$pageWrap.height($pageWrap.height());
baseHeight = $pageWrap.height() - $NavmainContent.height();
$("nav").delegate("a", "click", function() {
_link = $(this).attr("href");
history.pushState(null, null, _link);
loadContent(_link);
return false;
});
function loadContent(href){
$mainContent
.find("#guts")
.fadeOut(1000, function() {
$mainContent.hide().load(href + " #guts", function() {
$mainContent.fadeIn(1000);
});
});
$NavmainContent
.find('#inner-cn')
.find("#project-pagetools,#project-tools,#cp,#currentSection")
.animate({
'width': '0'
},800);
setTimeout(function(){
$first.load(href + " #currentSection span");
$second.load(href + " #project-tools li");
$third.load(href + " #project-pagetools li");
$fourth.load(href + " #cp span");
},900);
setTimeout(function(){
var l = $("#project-tools li").length ;
l = 100 / l ;
l = l + '%';
$("#project-tools li").css({
'width': l
});
$NavmainContent
.find("#project-pagetools,#project-tools")
.animate({
'width': '15%'
},800);
$NavmainContent
.find("#currentSection")
.animate({
'width': '10%'
},800);
$NavmainContent
.find("#cp")
.animate({
'width': '55%'
},800);
},1100);
}
});