图片自适应大小

图片自适应大小

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>
<html xmlns=””>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<script type=”text/javascript”>
var flag=false;
function DrawImage(ImgD,imageWidth,imageHeight) {
   var image=new Image();
   image.src=ImgD.src;
   if(image.width>0 && image.height>0){
    flag=true;
    if(image.width/image.height>= imageWidth/imageHeight){
     if(image.width>imageWidth){
     ImgD.width=imageWidth;
     ImgD.height=(image.height*imageWidth)/image.width;
     }else{
     ImgD.width=image.width;
     ImgD.height=image.height;
     }
     }
    else{
     if(image.height>imageHeight){
     ImgD.height=imageHeight;
     ImgD.width=(image.width*imageHeight)/image.height;
     }else{
     ImgD.width=image.width;
     ImgD.height=image.height;
     }
     }
    }
  }
 
</script>
<title>Anddecor</title>
</head>
<body>
<img alt=”” src=”pic01.gif” onload= “javascript:DrawImage(this,150,130)” />/*定义图片宽不超过150,高不超过130*/
</body>
</html>

图片自适应大小

相关文章:

你感兴趣的文章:

标签云: