js代码:
01 |
<script language= "" javascript "" type= "" text/javascript "" > |
05 |
function DrawImage(ImgD,FitWidth,FitHeight) |
07 |
var image= new Image(); |
09 |
if (image.width>0 && image.height>0) |
11 |
if (image.width/image.height>= FitWidth/FitHeight) |
13 |
if (image.width>FitWidth) |
16 |
ImgD.height=(image.height*FitWidth)/image.width; |
20 |
ImgD.width=image.width; |
21 |
ImgD.height=image.height; |
26 |
if (image.height>FitHeight) |
28 |
ImgD.height=FitHeight; |
29 |
ImgD.width=(image.width*FitHeight)/image.height; |
33 |
ImgD.width=image.width; |
34 |
ImgD.height=image.height; |
调用方式:
1 |
< img src = "" 1148202890.jpg"" alt = "" 自动缩放后的效果"" onload = "" javascript:DrawImage(this,200,200);"" /> |
如果图片较大,建议在图片标签里面同时设置期望的图片大小,这样不会导致页面在加载中撑开,该大小不会影响最终缩放效果。可以修改上面的代码为:
1 |
< img src = "" 1148202890.jpg"" alt = "" 自动缩放后的效果"" width = "" 200"" height = "" 200"" onload = "" javascript:DrawImage(this,200,200);"" /> |