`

让图片根据窗口大小按比例缩放

阅读更多

带有Frame的页面:

<script language="JavaScript">
  
  if(window.frameElement!=null)
  {
      window.frameElement.attachEvent("onresize",winResize);
  }
  else
  {
      window.attachEvent("onresize",winResize);
  }
  function winResize()
  {
      var img=document.getElementById("img");
      DrawImage(img);
  }
   
    <!--  
    //图片按比例缩放  
    var   flag=false;  
    function   DrawImage(ImgD){
     var   image=new   Image();  
     var   iwidth   =   document.body.clientWidth*0.95;    //定义允许图片宽度  
     var   iheight   =   document.body.clientHeight ;   //定义允许图片高度
    
     image.src=ImgD.src;  
     if(image.width>0   &&   image.height>0){  
      flag=true;  
      if(image.width/image.height>=   iwidth/iheight){  
       if(image.width>iwidth){      
        ImgD.width=iwidth;  
        ImgD.height=(image.height*iwidth)/image.width;  
       }else{  
        ImgD.width=image.width;      
        ImgD.height=image.height;  
       }  
         ImgD.alt=image.width+"×"+image.height;  
        }else{  
       if(image.height>iheight){      
        ImgD.height=iheight;  
        ImgD.width=(image.width*iheight)/image.height;  
       }else{  
        ImgD.width=image.width;      
        ImgD.height=image.height;  
       }  
        //ImgD.alt=image.width+"×"+image.height;  
      }  
     }  
    }    
    //-->
  </script>

 

<body>

      <img id="img"  src="<%=url%>" onload="DrawImage(this)" alt=""/>

</body>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics