2012-01-16 87 views
0

我是新來的開發jQuery代碼和我需要一些幫助縮短如下:縮短JQuery的動畫代碼

$(".colorblue").mouseup(function(){ 
    $("#phone").fadeTo('fast', 0.1, "swing"); 
    $("#phone").fadeTo('fast', 0.1, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("body").removeClass("blank , green , orange , yellow").delay(400).queue(function(next){ 
     $(this).addClass("blue"); 
     next(); 
    }); 
}); 

$(".colorgreen").mouseup(function(){ 
    $("#phone").fadeTo('fast', 0.01, "swing"); 
    $("#phone").fadeTo('fast', 0.01, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("body").removeClass("blank , blue , orange , yellow").delay(400).queue(function(next){ 
     $(this).addClass("green"); 
     next(); 
    }); 
}); 

$(".colororange").mouseup(function(){ 
    $("#phone").fadeTo('fast', 0.01, "swing"); 
    $("#phone").fadeTo('fast', 0.01, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("body").removeClass("blank , blue , green , yellow").delay(400).queue(function(next){ 
     $(this).addClass("orange"); 
     next(); 
    }); 
}); 

$(".coloryellow").mouseup(function(){ 
    $("#phone").fadeTo('fast', 0.01, "swing"); 
    $("#phone").fadeTo('fast', 0.01, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("body").removeClass("blank , blue , orange , green").delay(400).queue(function(next){ 
     $(this).addClass("yellow"); 
     next(); 
    }); 
}); 

$(".colorblank").mouseup(function(){ 
    $("#phone").fadeTo('fast', 0.01, "swing"); 
    $("#phone").fadeTo('fast', 0.01, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("body").removeClass("yellow , blue , orange , green").delay(400).queue(function(next){ 
     $(this).addClass("blank"); 
     next(); 
    }); 
}); 

// Menu changer 
$(".screenblue").mouseup(function(){ 
    $("#menu").fadeTo('fast', 0.1, "swing"); 
    $("#menu").fadeTo('fast', 0.1, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("#wrapper").removeClass("menublank , menugreen , menuorange , menuyellow").delay(400).queue(function(next){ 
     $(this).addClass("menublue"); 
     next(); 
    }); 
}); 

$(".screengreen").mouseup(function(){ 
    $("#menu").fadeTo('fast', 0.01, "swing"); 
    $("#menu").fadeTo('fast', 0.01, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("#wrapper").removeClass("menublank , menublue , menuorange , menuyellow").delay(400).queue(function(next){ 
     $(this).addClass("menugreen"); 
     next(); 
    }); 
}); 

$(".screenorange").mouseup(function(){ 
    $("#menu").fadeTo('fast', 0.01, "swing"); 
    $("#menu").fadeTo('fast', 0.01, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("#wrapper").removeClass("menublank , menublue , menugreen , menuyellow").delay(400).queue(function(next){ 
     $(this).addClass("menuorange"); 
     next(); 
    }); 
}); 

$(".screenyellow").mouseup(function(){ 
    $("#menu").fadeTo('fast', 0.01, "swing"); 
    $("#menu").fadeTo('fast', 0.01, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("#wrapper").removeClass("menublank , menublue , menuorange , menugreen").delay(400).queue(function(next){ 
     $(this).addClass("menuyellow"); 
     next(); 
    }); 
}); 

$(".screenblank").mouseup(function(){ 
    $("#menu").fadeTo('fast', 0.01, "swing"); 
    $("#menu").fadeTo('fast', 0.01, "swing").delay(50).queue(function(next){ 
      $(this).fadeTo('fast', 1, "swing"); 
       next(); 
    }); 
    $("#wrapper").removeClass("menuyellow , menublue , menuorange , menugreen").delay(400).queue(function(next){ 
     $(this).addClass("menublank"); 
     next(); 
    }); 
}); 

我一直在使用這麼多$聽說是不寫jQuery代碼的好方法 - 它只是控制一些瀏覽器動畫。

任何幫助表示讚賞。

謝謝。

下面是HTML:

當然,這裏是HTML:

 <div id="phone"><div id="menu"></div></div><!--phone--> 
     <div id="centercontainer"> 
      <br class="break" /> 
     </div> 
     <div id="footer"> 
      <div id="colorchanger" class="device"> 
       <h3>Device</h3> 
       <a class="colorbox colorblue" href="#" title="Blue "></a> 
       <a class="colorbox colorgreen" href="#" title="Green "></a> 
       <a class="colorbox colororange" href="#" title="Orange "></a> 
       <a class="colorbox coloryellow" href="#" title="Yellow "></a> 
       <a class="colorbox colorblank reset" href="#" title="Blank "></a> 
      </div> 
      <div id="colorchanger" class="screen"> 
       <h3>Screen</h3> 
       <a class="colorbox screenblue" href="#" title="Blue "></a> 
       <a class="colorbox screengreen" href="#" title="Green "></a> 
       <a class="colorbox screenorange" href="#" title="Orange "></a> 
       <a class="colorbox screenyellow" href="#" title="Yellow "></a> 
       <a class="colorbox screenblank reset" href="#" title="Blank "></a> 
      </div> 
     </div> 
+1

*我一直在使用這麼多$聽到的是不是一個編寫JQuery代碼的好方法*:不一定。更重要的是,你一遍又一遍地重複同一段代碼。您能否提供一個HTML相關部分的例子? – 2012-01-16 10:53:55

+0

您可以通過改變所有的$(「#菜單」)和$(「#電話」)到一個變量,而不是每次都使用jQuery選擇開始。 – JNDPNT 2012-01-16 10:57:23

回答

0

你可以把它短得多

var colors = new Array('blue', 'green', 'orange', 'yellow', 'blank'); 

$(".colorblue, .colorgreen, .colororange, .coloryellow .colorblank") 
.mouseup(function(){ 
    var $phone = $("#phone"), $this = $(this), $body = $("body"), idx, i; 

    $phone.fadeTo('fast', 0.1, "swing"); 
    $phone.fadeTo('fast', 0.1, "swing").delay(50).queue(function(next){ 
     $(this).fadeTo('fast', 1, "swing"); 
     next(); 
    }); 

    for(i = 0; i < colors.length; i ++) { 
     if($this.hasClass('color' + colors[i])) 
      idx = i; 
     else $body.removeClass(colors[i]); 
    } 

    $body.delay(400).queue(function(next) { 
     $(this).addClass(colors[idx]); 
     next(); 
    }); 
}); 

$(".screenblue, .screengreen, .screenorange, .screenyellow, .screenblank") 
.mouseup(function(){ 
    var $menu = $("#menu"), $this = $(this), $wrapper = $("#wrapper"), idx, i; 
    $menu.fadeTo('fast', 0.1, "swing"); 
    $menu.fadeTo('fast', 0.1, "swing").delay(50).queue(function(next){ 
     $(this).fadeTo('fast', 1, "swing"); 
     next(); 
    }); 

    for(i = 0; i < colors.length; i ++) { 
     if($this.hasClass('screen' + colors[i])) 
      idx = i; 
     else $wrapper.removeClass('menu' + colors[i]); 
    } 

    $wrapper.delay(400).queue(function(next){ 
     $(this).addClass('menu' + colors[idx]); 
     next(); 
    }); 
}); 
+0

非常感謝這一點,因爲我放在一起的原代碼,我可以看到發生了什麼事情,如果你有一秒鐘你能解釋IDX這裏的使用情況如何? – 2012-01-16 11:29:24

+0

'idx'指示屏幕或身體是什麼顏色。由於您將mouseup事件綁定到所有這些類,因此您無法找到哪個類使事件發生。因此,'for'語句中'idx' rememebers,它已經擁有,讓你可以在以後添加類的顏色類。 – Sang 2012-01-16 11:38:23

+0

@ChristopherCamplin不要忘記檢查這個答案,如果它確實能幫助你:) – Sang 2012-01-16 11:40:53