I have a series of panels I'm animating in a Metro style for a website. The problem is, I'm not seeing a good way to do the below in fewer lines of code. How can I do something like this in a more compact elegant fashion?
$('#metroNow').click(function() {
$("#c1").animate({marginLeft: 0},750,"easeOutCirc");
$("#c2").animate({marginLeft: 0},750,"easeOutCirc");
$("#c3").animate({marginLeft: 0},750,"easeOutCirc");
$("#c4").animate({marginLeft: 0},750,"easeOutCirc");
$("#c5").animate({marginLeft: 0},750,"easeOutCirc");
$('#fake').animate({backgroundPosition: 0}, 1550, "easeOutCirc");
$('.metroNavSpan').removeClass('metroNavSpanSelected');
$('#metroNow').addClass('metroNavSpanSelected');
});
$('#metroPeople').click(function() {
$("#c1").animate({marginLeft: -1040},750,"easeOutCirc");
$("#c2").animate({marginLeft: 0},750,"easeOutCirc");
$("#c3").animate({marginLeft: 0},750,"easeOutCirc");
$("#c4").animate({marginLeft: 0},750,"easeOutCirc");
$("#c5").animate({marginLeft: 0},750,"easeOutCirc");
$('#fake').animate({backgroundPosition: -65}, 1550, "easeOutCirc");
$('.metroNavSpan').removeClass('metroNavSpanSelected');
$('#metroPeople').addClass('metroNavSpanSelected');
});
$('#metroInfopedia').click(function() {
$("#c1").animate({marginLeft: -1040},750,"easeOutCirc");
$("#c2").animate({marginLeft: -1040},750,"easeOutCirc");
$("#c3").animate({marginLeft: 0},750,"easeOutCirc");
$("#c4").animate({marginLeft: 0},750,"easeOutCirc");
$("#c5").animate({marginLeft: 0},750,"easeOutCirc");
$('#fake').animate({backgroundPosition: -130}, 1550, "easeOutCirc");
$('.metroNavSpan').removeClass('metroNavSpanSelected');
$('#metroInfopedia').addClass('metroNavSpanSelected');
});
$('#metroVideos').click(function() {
$("#c1").animate({marginLeft: -1040},750, "easeOutCirc");
$("#c2").animate({marginLeft: -1040},750,"easeOutCirc");
$("#c3").animate({marginLeft: -1040},750,"easeOutCirc");
$("#c4").animate({marginLeft: 0},750,"easeOutCirc");
$("#c5").animate({marginLeft: 0},750,"easeOutCirc");
$('#fake').animate({backgroundPosition: -195}, 1550, "easeOutCirc");
$('.metroNavSpan').removeClass('metroNavSpanSelected');
$('#metroVideos').addClass('metroNavSpanSelected');
});
$('#metroAbout').click(function() {
$("#c1").animate({marginLeft: -1040},750,"easeOutCirc");
$("#c2").animate({marginLeft: -1040},750,"easeOutCirc");
$("#c3").animate({marginLeft: -1040},750,"easeOutCirc");
$("#c4").animate({marginLeft: -1040},750,"easeOutCirc");
$("#c5").animate({marginLeft: 0},750,"easeOutCirc");
$('#fake').animate({backgroundPosition: -260}, 1550, "easeOutCirc");
$('.metroNavSpan').removeClass('metroNavSpanSelected');
$('#metroAbout').addClass('metroNavSpanSelected');
});
Here: your 50 liner into 9 lines of code.
If your #c1, #c2.. .and so on have a same parent
add to all of them a class '.el' Ex: <div id="c2" class="el"></div>
var eoc = "easeOutCirc",
mNSS = 'metrsoNavSpanSelected';
$('.metroNavSpan').click(function(){
var ind = $(this).index();
$('.el').eq(ind).prevAll().animate({marginLeft: -1040},750,eoc).nextAll().andSelf().animate({marginLeft: 0},750,eoc);
$('#fake').animate({backgroundPosition: '-='+(65*ind) },1550,eoc);
$('.metroNavSpan').removeClass(mNSS);
$(this).addClass(mNSS);
});