博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

图片等比例缩放

Posted on 2012-12-26 16:17  人生梦想起飞  阅读(243)  评论(0编辑  收藏  举报

/*
  *图片缩放
  */
  var showImg = function(img,maxWidth,maxHeight){
  var rate=(maxHeight/img.height>maxWidth/img.width?maxWidth/img.width:maxHeight/img.height);
    if(rate<1){
     img.width=img.width*rate;
     img.height=img.height*rate;
    }
     return img;
  };   
 
         function images_WH(){
   
    var imgs =new Image();
            /**
         **图片超出等比例缩放
         **/
       var imagID=document.getElementById("imgID");
       imgs.onload = function(){
         var FitWidth=894,FitHeight=435;
         showImg(this, FitWidth, FitHeight);
        imagID.width = imgs.width;
        imagID.height = imgs.height;
        imagID.style.display='';
       };
       imgs.src=imagID.src;
   
    }