I'm working on an app that uses a switch statement to provide custom animations depending on which link has been clicked to trigger the animation. It seems like a lot of code to get what I want, but I'm having a little trouble figuring out a better way to do it.
What happens is that when you click on a link, a div will open up to reveal hidden content and the other divs will slide to the side of the screen. Kinda like a custom accordian.
My switch statement looks like this - the history paramater is taken from the id of the clicked link. The divs are stored in an object called rpsObject.
switch( history ) {
case "biography" :
$("#" + rpsObject.boxId[1]).myAnimation();
$("#" + rpsObject.boxId[2]).myAnimation({ top : 80 });
$("#" + rpsObject.boxId[3]).myAnimation({ top: 160 });
$("#" + rpsObject.boxId[4]).myAnimation({ top: 240 });
$("#" + rpsObject.boxId[5]).fadeInCloseLink();
break;
case "blog" :
$("#" + rpsObject.boxId[0]).myAnimation();
$("#" + rpsObject.boxId[2]).myAnimation({ top : 80 });
$("#" + rpsObject.boxId[3]).myAnimation({ top: 160 });
$("#" + rpsObject.boxId[4]).myAnimation({ top: 240 });
$("#" + rpsObject.boxId[5]).fadeInCloseLink();
break;
case "diary" :
$("#" + rpsObject.boxId[0]).myAnimation();
$("#" + rpsObject.boxId[1]).myAnimation({ top : 80 });
$("#" + rpsObject.boxId[3]).myAnimation({ top: 160 });
$("#" + rpsObject.boxId[4]).myAnimation({ top: 240 });
$("#" + rpsObject.boxId[5]).fadeInCloseLink();
break;
case "reviews" :
$("#" + rpsObject.boxId[0]).myAnimation();
$("#" + rpsObject.boxId[1]).myAnimation({ top : 80 });
$("#" + rpsObject.boxId[2]).myAnimation({ top: 160 });
$("#" + rpsObject.boxId[4]).myAnimation({ top: 240 });
$("#" + rpsObject.boxId[5]).fadeInCloseLink();
break;
case "images" :
$("#" + rpsObject.boxId[0]).myAnimation();
$("#" + rpsObject.boxId[1]).myAnimation({ top : 80 });
$("#" + rpsObject.boxId[2]).myAnimation({ top: 160 });
$("#" + rpsObject.boxId[3]).myAnimation({ top: 240 });
$("#" + rpsObject.boxId[5]).fadeInCloseLink();
break;
case "contact" :
$("#" + rpsObject.boxId[0]).myAnimation();
$("#" + rpsObject.boxId[1]).myAnimation({ top : 80 });
$("#" + rpsObject.boxId[2]).myAnimation({ top: 160 });
$("#" + rpsObject.boxId[3]).myAnimation({ top: 240 });
$("#" + rpsObject.boxId[4]).fadeInCloseLink();
break;
}
Hopefully it should be pretty obvious what I'm doing here!
The functions myAnimation() and fadeinCloseLink() are custom functions. The latter must be performed on the last item of the object, which on complete trigger a custom animation for the selected div. The function fadeinCloseLink() does the following:
$.fn.fadeInCloseLink = function() {
$(this).animate({ "left" : "640px", "top" : "320px", "height" : "80px" }, 300,
function(){
disFull.animate({ "opacity" : "toggle", "height" : "toggle" }, 500);
});
}
Where disFull refers to the div that will be affected.
Hopefully this is clear enough to get my question across.