以下是放在img标签的onload事件中的例如:<img src="icon/loading.gif" onload="DrawImage(this,120,100,true,'http://bbs.csdn.net/images/123.jpg')">
此方法在ie6、ie7、ie8中运行要报错:stack overflow at line:0 function DrawImage(ImgD,iwidth,iheight,IsCenter,url){
//参数(图片,允许的宽度,允许的高度)
var image=new Image();
//image.src=""; /*先清空下缓存,ie中防止加载的不是最新的图片*/
image.src=typeof url=="undefined"||url==""||url==null?ImgD.src:url;
ImgD.src = image.src;
if(image.width>0 && image.height>0){
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;
}
}else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
if((IsCenter)){
ImgD.style.marginTop = ((iheight-ImgD.height)/2)+"px"; /*自动居中*/
}
}
//ImgD.src = image.src;
}
此方法在ie6、ie7、ie8中运行要报错:stack overflow at line:0 function DrawImage(ImgD,iwidth,iheight,IsCenter,url){
//参数(图片,允许的宽度,允许的高度)
var image=new Image();
//image.src=""; /*先清空下缓存,ie中防止加载的不是最新的图片*/
image.src=typeof url=="undefined"||url==""||url==null?ImgD.src:url;
ImgD.src = image.src;
if(image.width>0 && image.height>0){
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;
}
}else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
if((IsCenter)){
ImgD.style.marginTop = ((iheight-ImgD.height)/2)+"px"; /*自动居中*/
}
}
//ImgD.src = image.src;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货