天天看點

JS控制圖檔顯示的大小(圖檔等比例縮放)

<HTML>

<HEAD>

<TITLE> New Document </TITLE>

<script language="JavaScript">

<!--

var flag=false;

function DrawImage(ImgD){

 var image=new Image();

 image.src=ImgD.src;

 if(image.width>0 && image.height>0){

  flag=true;

  if(image.width/image.height>= 164/112){

   if(image.width>164){

    ImgD.width=164;

    ImgD.height=(image.height*164)/image.width;

   }else{

    ImgD.width=image.width;

    ImgD.height=image.height;

   }

   ImgD.alt=image.width+"x"+image.height;

  }

  else{

   if(image.height>112){

    ImgD.height=112;

    ImgD.width=(image.width*112)/image.height;

 }

}

//-->

</script>

</HEAD>

<BODY>

<a href="./img.jpg" target="_blank"><img src="./img.jpg" border="0" width="164" height="112" onload="javascript:DrawImage(this);"></a>

</BODY>

</HTML>

繼續閱讀