doubleyong
管理员
管理员
  • 最后登录2026-05-25
  • 发帖数1198
  • 最爱沙发
  • 喜欢达人
  • 原创写手
  • 社区居民
  • 忠实会员
阅读:6962回复:0

js 图片按比例缩放

楼主#
更多 发布于:2016-10-26 09:32
<script language="JavaScript">  
<!--  
//图片按比例缩放  
var flag=false;  
function DrawImage(ImgD){  
var image=new Image();  
var iwidth = 80;  //定义允许图片宽度  
var iheight = 80;  //定义允许图片高度  
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>  
<img src=".." onload = "DrawImage(this)">  



转自:http://www.jb51.net/article/583_4.htm
         此链接有很多的JS小技巧,更多的技巧点击上面的链接进入
知识需要管理,知识需要分享
游客


返回顶部

公众号

公众号