$.fn.imgauto = function(co){ function letter (fn) { if (typeof arguments[0] != 'function') return; fn(); var times = typeof arguments[1] != 'number'?300:arguments[1]; var timer = null; var st = new date().gettime(); return function () { var et = new date().gettime(); if (et - st < times) { cleartimeout(timer); timer = settimeout(function () { //return fn.apply(this,arguments); fn(); }, times); } else{ cleartimeout(timer); st = et; fn(); }; }; }; $(this).each(function(){ var t = $(this); var cover = t.attr('img-auto')=='cover'||co?true:false; var img = new image(); img.src = t.attr('src'); t.css('opacity',0); if (t.attr('no-box')) { var box = $('
'); box.css({ "width":t.attr('width'), "height":t.attr('height'), "text-align":'left', "overflow":'hidden' }) t.wrapall(box); } else{ var box = t.parent(); }; function move (){ if (img.width>0 || img.height>0) { if ( img.width/img.height < box.width()/box.height() ) { if (cover) { t.css({'width':'100%','height':'auto','margin':0}).css('margin-top', -(t.height()-box.height())/2) .parent().css('overflow', 'hidden'); }else{ t.css({'width':'auto','height':'100%','margin':0}).css('margin-left', (box.width()-t.width())/2); }; }else{ if (cover) { t.css({'width':'auto','height':'100%','margin':0}).css('margin-left', -(t.width()-box.width())/2) .parent().css('overflow', 'hidden'); }else{ t.css({'width':'100%','height':'auto','margin':0}).css('margin-top', (box.height()-t.height())/2); }; }; }else{ settimeout(move); } }; move(); var throttled = letter(move); $(window).resize(throttled); t.fadeto(2000, 1); }); return this; };