2012-04-07 22 views
1

我有一個我已經收緊和縮小的jQuery腳本。我不喜歡鏈接到一個CDN jQuery腳本,該腳本花費了HTTP請求以及40-90kb的客戶端下載。從jQuery中刪除未使用的變量

我聽說可以調整新的Google閉包編譯器以從jQuery中刪除未使用的變量以減小大小。我瞭解使用CDN鏈接的優點(就像谷歌),但由於我只使用這個代碼的單個頁面,我寧願從jQuery中刪除未使用的變量,並自己託管它。

我使用jQuery 1.5.2,這裏是我的腳本:

(function(a){a.fn.countDown=function(b){config={};a.extend(config,b);diffSecs=this.setCountDown(config);config.onComplete&&a.data(a(this)[0],"callback",config.onComplete);config.omitWeeks&&a.data(a(this)[0],"omitWeeks",config.omitWeeks);a("#"+a(this).attr("id")+" .digit").html('<div class="top"></div><div class="bottom"></div>');a(this).doCountDown(a(this).attr("id"),diffSecs,500);return this};a.fn.stopCountDown=function(){clearTimeout(a.data(this[0],"timer"))};a.fn.startCountDown=function(){this.doCountDown(a(this).attr("id"), a.data(this[0],"diffSecs"),500)};a.fn.setCountDown=function(b){var c=new Date;b.targetDate?c=new Date(b.targetDate.month+"/"+b.targetDate.day+"/"+b.targetDate.year+" "+b.targetDate.hour+":"+b.targetDate.min+":"+b.targetDate.sec+(b.targetDate.utc?" UTC":"")):b.targetOffset&&(c.setFullYear(b.targetOffset.year+c.getFullYear()),c.setMonth(b.targetOffset.month+c.getMonth()),c.setDate(b.targetOffset.day+c.getDate()),c.setHours(b.targetOffset.hour+c.getHours()),c.setMinutes(b.targetOffset.min+c.getMinutes()), c.setSeconds(b.targetOffset.sec+c.getSeconds()));diffSecs=Math.floor((c.valueOf()-(new Date).valueOf())/1E3);a.data(this[0],"diffSecs",diffSecs);return diffSecs};a.fn.doCountDown=function(b,c,d){$this=a("#"+b);0>=c&&(c=0,a.data($this[0],"timer")&&clearTimeout(a.data($this[0],"timer")));secs=c%60;mins=Math.floor(c/60)%60;hours=Math.floor(c/60/60)%24;days=!0==a.data($this[0],"omitWeeks")?Math.floor(c/60/60/24):Math.floor(c/60/60/24)%7;weeks=Math.floor(c/60/60/24/7);$this.dashChangeTo(b,"seconds_dash", secs,d?d:800);$this.dashChangeTo(b,"minutes_dash",mins,d?d:1200);$this.dashChangeTo(b,"hours_dash",hours,d?d:1200);$this.dashChangeTo(b,"days_dash",days,d?d:1200);$this.dashChangeTo(b,"weeks_dash",weeks,d?d:1200);a.data($this[0],"diffSecs",c);0<c?(e=$this,t=setTimeout(function(){e.doCountDown(b,c-1)},1E3),a.data(e[0],"timer",t)):(cb=a.data($this[0],"callback"))&&a.data($this[0],"callback")()};a.fn.dashChangeTo=function(b,c,d,m){$this=a("#"+b);for(b=$this.find("."+c+" .digit").length-1;0<=b;b--){var j= d%10,d=(d-j)/10;$this.digitChangeTo("#"+$this.attr("id")+" ."+c+" .digit:eq("+b+")",j,m)}};a.fn.digitChangeTo=function(b,c,d){d||(d=800);a(b+" div.top").html()!=c+""&&(a(b+" div.top").css({display:"none"}),a(b+" div.top").html(c?c:"0").slideDown(d),a(b+" div.bottom").animate({height:""},d,function(){a(b+" div.bottom").html(a(b+" div.top").html());a(b+" div.bottom").css({display:"block",height:""});a(b+" div.top").hide().slideUp(10)}))}})(jQuery); jQuery(document).ready(function(){$("#countdown").countDown({targetDate:{day:7,month:5,year:2012,hour:8,min:0,sec:0,utc:!0}})});$(document).ready(function(){$("#xtreme_slider").tinycarousel()}); (function(a){function b(c,b){function m(){b.controls&&0<n.length&&0<o.length&&(n.click(function(){g.move(-1);return!1}),o.click(function(){g.move(1);return!1}));b.interval&&c.hover(g.stop,g.start);b.pager&&0<p.length&&a("a",p).click(x)}function j(){b.controls&&(n.toggleClass("disable",!(0<f)),o.toggleClass("disable",!(f+1<h)));if(b.pager){var c=a(".pagenum",p);c.removeClass("active");a(c[f]).addClass("active")}}function x(){a(this).hasClass("pagenum")&&g.move(parseInt(this.rel),!0);return!1}function v(){b.interval&& !q&&(clearTimeout(r),r=setTimeout(function(){f=f+1==h?-1:f;s=f+1==h?!1:0==f?!0:s;g.move(s?1:-1)},b.intervaltime))}var g=this,w=a(".viewport:first",c),u=a(".overview:first",c),i=u.children(),o=a(".next:first",c),n=a(".prev:first",c),p=a(".pager:first",c),k,h,f,r,q,s=!0,l="x"==b.axis;this.stop=function(){clearTimeout(r);q=!0};this.start=function(){q=!1;v()};this.move=function(a,c){f=c?a:f+=a;if(-1<f&&f<h){var g={};g[l?"left":"top"]=-(f*k*b.display);u.animate(g,{queue:!1,duration:b.animation?b.duration: 0,complete:function(){"function"==typeof b.callback&&b.callback.call(this,i[f],f)}});j();v()}};return function(){k=l?a(i[0]).outerWidth(!0):a(i[0]).outerHeight(!0);var c=Math.ceil((l?w.outerWidth():w.outerHeight())/(k*b.display)-1);h=Math.max(1,Math.ceil(i.length/b.display)-c);f=Math.min(h,Math.max(1,b.start))-2;u.css(l?"width":"height",k*i.length);g.move(1);m();return g}()}a.tiny=a.tiny||{};a.tiny.carousel={options:{start:1,display:1,axis:"x",controls:!1,pager:!0,interval:!0,intervaltime:5E3,rewind:!1, animation:!0,duration:1E3,callback:null}};a.fn.tinycarousel=function(c){c=a.extend({},a.tiny.carousel.options,c);this.each(function(){a(this).data("tcl",new b(a(this),c))});return this};a.fn.tinycarousel_start=function(){a(this).data("tcl").start()};a.fn.tinycarousel_stop=function(){a(this).data("tcl").stop()};a.fn.tinycarousel_move=function(b){a(this).data("tcl").move(b-1,!0)}})(jQuery); 

可有人請根據我的腳本的jQuery刪除未使用的代碼?

+0

儘管您觸及了一些潛在的問題,但您從未明確說出您的問題是什麼? – Corbin 2012-04-07 04:23:36

+0

另外,Google Closure雖然有用,但可能並不是最好的主意,但是如果代碼不是從頭開始編寫的,那麼YMMV – ZenMaster 2012-04-07 04:28:00

+0

如果不清楚,我很遺憾。我已經編輯了一個更清晰的問題的帖子.. :) – Umer 2012-04-07 04:28:02

回答

1

您可以通過修改其構建腳本來嘗試自定義jquery構建(source on github)。如果可以的話,你也可以試試Zepto。這個庫非常輕量級,並且具有jQuery兼容的API。

另外,related question

+0

現在對我來說太複雜了! :( – Umer 2012-04-07 04:31:32

1

Google Closure可以容易地通過〜59%上簡單減少代碼,或者在先進的〜65%。我建議嘗試兩種方法,並在生產服務器上使用它們之前檢查它們是否都起作用,但這似乎是您想要的。

而且,在編譯時,一定要使用jQuery庫使用頂部的選擇框進行編譯。