JS 图片等比缩放

       JS代码

<script type="text/javascript">
	function ChangeImage(mw,mh,oImg){
		var img = new Image(); 
		img.src = oImg.src;
		var hRatio;  
		var wRatio; 
		var Ratio = 1;  
		var w = img.width;   
		var h = img.height;  
		wRatio = mw / w;  
		hRatio = mh / h; 
		if (mw ==0 && mh==0){   
			Ratio = 1;          
		}else if (mw==0){  
			if (hRatio<1) Ratio = hRatio;  
		}else if (mh==0){  
			if (wRatio<1) Ratio = wRatio;
		}else if (wRatio<1 || hRatio<1){  
			Ratio = (wRatio<=hRatio?wRatio:hRatio);  
		}
		if (Ratio<1){   
			w = w * Ratio;
			h = h * Ratio;
		}
		objImg.height = h;
		objImg.width = w;
	}
</script>

  调用代码

<img src="12.jpg" border="0" width="0" height="0" onload="ChangeImage(534,800,this)" alt="534 X 800"/>

  

 

posted on 2014-06-05 09:40  小菜鸟成长记  阅读(256)  评论(0)    收藏  举报

导航