在不确定img大小的情况下实现等比缩放

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
<script language="javascript">
		function DrawImage(ImgD,kw,kh)
		{
			var image=new Image();
			image.src=ImgD.src;
			if(image.height<image.width)//说明宽》高==》以宽为标准
			{
				if(image.width>kw)
				{
					ImgD.width=kw;
					ImgD.height=(image.height*kw)/image.width;
				}
				else
				{
					ImgD.width=image.width; 
					ImgD.height=image.height;
				}
			}
			else//以高为标准
			{
				if(image.height>kh)
				{
					ImgD.height=kh;
					ImgD.width=(image.width*kh)/image.height;
				}
				else
				{
					ImgD.width=image.width; 
					ImgD.height=image.height;
				}
			}
		}
</script>
 <body>
  <img src="720X450/153008rf5acferkrlfkw5a.jpg" onload="javascript:DrawImage(this,'300','700');"/>
 </body>
</html>

DrawImage(ImgD,kw,kh)  第一个参数就是img本身  kw和kh是你设置的不超过的界限值  宽度:300  高度:700




你可能感兴趣的:(在不确定img大小的情况下实现等比缩放)