答案,從答案明顯略作修改:簡化我的JQuery混亂
var eoc = "easeOutCirc",mNSS = 'metrsoNavSpanSelected';
$('.metroNavSpan').click(function(){
var ind = $(this).index();
$('.content-box').eq(ind).prevAll().animate({marginLeft: -1040},750,eoc);
$('.content-box').eq(ind).nextAll().andSelf().animate({marginLeft: 0},750,eoc);
$('#fake').animate({backgroundPosition: (-65*ind)},1550,eoc);
$('.metroNavSpan').removeClass(mNSS);
$(this).addClass(mNSS);
});
的變化:上eastOutCirc拼寫是固定的:)
背景現在的位置是動畫從改變 - =(65 * IND),以只是(-65 * ind)。不希望這些變化是累積的,意味着面板應該是0,0;面板2應該始終是-65,0。 - =正在積累點擊量,以便在面板4和5之間來回切換會產生非常大的數字。
nextAll和prevAll是分開的,因爲保持它們在同一行中總是在動畫之後將面板回收到面板1。
原題:
我有一系列的面板,我在動畫Metro風格的網站。問題是,我沒有看到用較少的代碼行來完成下面的一個好方法。我怎樣才能以更緊湊優雅的方式做這樣的事情?
$('#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');
});
你應該嘗試[這裏]( http://codereview.stackexchange.com/) –