日期:2014-05-18  浏览次数:20760 次

图片等比缩放后为什么时大时小?
<script language="javascript"> 
var flag=false; 
function MyImage(Img){ 
var WIDTH=600;
var HEIGHT=600;
var image=new Image(); 
image.src=Img.src; 
width=WIDTH
height=HEIGHT;
if(image.width>width||image.height>height){
w=image.width/width; 
h=image.height/height; 
if(w>h){
Img.width=width; 

Img.height=image.height/w; 

}else{
Img.height=height; 

Img.width=image.width/h; 



</script> 
<img src='images/ppd14.jpg' onload='javascript:MyImage(this)' border='0'>
以上是代码,麻烦各位大侠帮忙解决下,谢谢了.

------解决方案--------------------
应该是你用onload='javascript:MyImage(this)'导入,网页就先加载原图片,再调用你的JS

代码改成如下试试
JScript code


<img src='Winter.jpg' id="im" border='0'> 
<script language="javascript"> 
var flag=false; 
function MyImage(Img){ 
var WIDTH=600; 
var HEIGHT=600; 
var image=new Image(); 
image.src=Img.src; 
width=WIDTH 
height=HEIGHT; 
if(image.width>width ||image.height>height){ 
w=image.width/width; 
h=image.height/height; 
if(w>h){ 
Img.width=width; 

Img.height=image.height/w; 

}else{ 
Img.height=height; 

Img.width=image.width/h; 
} 
} 
} 
MyImage(im);

</script>