一個圖片顯示的JS示例

1.html的代碼如下:

 

<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;
   }else{
    ImgD.width=image.width;
    ImgD.height=image.height;
   }
   ImgD.alt=image.width+"x"+image.height;
  }
 }
}
//-->
</script>
</HEAD>

<BODY>

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

</BODY>
</HTML>

 

圖片以固定的大小顯示,點擊後顯示原圖片。

注意:1.html要和1.jpg放在同一個文件夾下,那樣才能正確顯示圖片。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章