/* * 鍥剧墖瑁佸壀 * imgdom锛氬浘鐗嘾om瀵硅薄 涔熷彲浠ヤ紶this * cutimgtype(瑁佸壀褰撴椂): 灞呬腑瑁佸壀autocut 鎸夋瘮渚嬬缉鏀維caling 鎷変几stretching * cutimgparent: 鐖剁被閫夋嫨鍣?渚嬪div鏍囩鐨刬d鏄痬ydiv 閭d箞杩欓噷灏卞~ #mydiv * * 渚嬪锛? * cutimg(this,"autocut","#mydiv") //浼犵埗鏍囩 * cutimg(this,"autocut") //涓嶄紶鐖舵爣绛? */ function cutimg(imgdom,cutimgtype,cutimgparent){ $(imgdom).each(function(){ var $this=$(imgdom); $this.css("csstext",""); var objheight,objwidth; //ie7+娴忚鍣ㄩ兘鑳藉吋瀹圭殑鍑芥暟灏佽:鑾峰彇鍥剧墖鐨勫師濮嬪昂瀵? if($this[0].naturalwidth && $this[0].naturalheight) { objwidth = $this[0].naturalwidth; objheight = $this[0].naturalheight; } else { var img = new image(); img.src = $this[0].src; objwidth = img.width; objheight = img.height; } var parentheight,parentwidth; if (cutimgparent!=undefined){ parentheight=$this.parents(cutimgparent).eq(0).height();//鍥剧墖鐖跺鍣ㄩ珮搴? parentwidth=$this.parents(cutimgparent).eq(0).width();//鍥剧墖鐖跺鍣ㄥ搴? }else{ if($this.parent().prop('tagname')=='a' || $this.parent().prop('tagname')=='a'){ parentheight=$this.parent().parent().height();//鍥剧墖鐖跺鍣ㄩ珮搴? parentwidth=$this.parent().parent().width();//鍥剧墖鐖跺鍣ㄥ搴? }else{ parentheight=$this.parent().height();//鍥剧墖鐖跺鍣ㄩ珮搴? parentwidth=$this.parent().width();//鍥剧墖鐖跺鍣ㄥ搴? } } var imgcsstext = ""; if(cutimgtype == "autocut"){ var ratio=objheight/objwidth; if(objheight > objwidth) {//璧嬪€煎楂? if(parentwidth*ratio < parentheight){ imgcsstext = "width:"+parentheight/ratio+"px!important;height:"+parentheight+"px!important;"; }else{ imgcsstext = "width:"+parentwidth+"px!important;height:"+parentwidth*ratio+"px!important;"; } $this.css("csstext",imgcsstext); } else if(objheightparentwidth) {//璧嬪€煎楂? imgcsstext = "width:"+parentheight+"px!important;height:"+parentheight+"px!important;"; $this.css("csstext",imgcsstext); }else{ imgcsstext = "width:"+parentwidth+"px!important;height:"+parentwidth+"px!important;"; $this.css("csstext",imgcsstext); } } /* objheight=$this[0].height;//閲嶆柊鑾峰彇瀹介珮 objwidth=$this[0].width;*/ objheight = $this.css("height").replace("px",""); objwidth = $this.css("width").replace("px",""); if(objheight>objwidth) { if(objheight == parentheight) { imgcsstext = $this.attr("style")+";margin-left:"+(parentwidth-objwidth)/2+"px!important;border:0px!important;"; }else{ imgcsstext = $this.attr("style")+";margin-top:"+(parentheight-objheight)/2+"px!important;border:0px!important;"; } $this.css("csstext",imgcsstext); }else if(objheight= parentwidth) { imgcsstext = $this.attr("style")+";margin-left:"+(parentwidth-objwidth)/2+"px!important;border:0px!important;" $this.css("csstext",imgcsstext); }else{ imgcsstext = $this.attr("style")+";margin-top:"+(parentheight-objheight)/2+"px!important;border:0px!important;"; $this.css("csstext",imgcsstext); } } }else{ try { var widthrate = objwidth / parentwidth; var heightrate = objheight / parentheight; var imgwidth = 0; var imgheight = 0; if(widthrate > 1 || heightrate > 1 || widthrate < 1 || heightrate < 1) { if(cutimgtype == "scaling")//鎸夋瘮渚嬬缉鏀? { var rate = math.max(widthrate, heightrate); imgwidth = math.max(1, math.min(objwidth/rate, parentwidth)); imgheight = math.max(1, math.min(objheight/rate, parentheight)); } else//鎷変几 { imgwidth = parentwidth; imgheight = parentheight; } } else //澶у皬鍚堥€? { imgwidth = parentwidth; imgheight = parentheight; } imgcsstext += "width:"+imgwidth+"px!important;height:"+imgheight+"px!important;"; $this.css("csstext",imgcsstext); if(imgwidth>imgheight) { if(imgwidth != parentwidth){ imgcsstext = $this.attr("style")+";margin-left:"+(parentwidth-imgwidth)/2+"px!important;border:0px!important;"; }else{ imgcsstext = $this.attr("style")+";margin-top:"+(parentheight-imgheight)/2+"px!important;border:0px!important;"; } $this.css("csstext",imgcsstext); } else { if(imgheight != parentheight){ imgcsstext = $this.attr("style")+";margin-top:"+(parentheight-imgheight)/2+"px!important;border:0px!important;"; }else{ imgcsstext = $this.attr("style")+";margin-left:"+(parentwidth-imgwidth)/2+"px!important;border:0px!important;"; } $this.css("csstext",imgcsstext); } } catch(e) { } } }) }