不说那么多,在没有出现IE8时,一直都在用的图片缩小JS函数,从没出过错。
//此函数在网上到处可见,以前也一直都用,在火狐和谷歌浏览器中没有问题。
function DrawImage(ImgD,width,height){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
//alert(image.width);
//flag=true;
if(image.width/image.height>= width/height){
if(image.width>width){
ImgD.width=width;
ImgD.height=(image.height*height)/image.width;
}
else
{
ImgD.width=image.width;
ImgD.height=image.height;
}
/*ImgD.alt="bigpic" */
}
else{
if(image.height>height){
ImgD.height=height;
ImgD.width=(image.width*height)/image.height;
}
else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
这是在一次偶然情况下,发现一张同样的图片,IE8却出现不同的缩小比例,差点让我在给客户做演示时出丑,重新写过此JS更简洁。
但这并不是说上面的那个函数有错,而是它在IE8上和表格套在一起出错问题。但下面这个不会出现。
//自动缩小图片,ImgD为img,width为要缩小到的宽度,height为要缩小到的高度。按比例缩放。
//调用示例<img src="http://www.9mge.com/image/common/myselfpp.jpg" onload="DrawImage(this,50,50);">
function DrawImage(ImgD, width, height) {
var image = new Image();
image.src = ImgD.src;
//当存在图片时,且宽度大于要缩小的或高度大于要缩小的
if (image.width > 0 && image.height > 0 && (image.width > width || image.height > height)) {
if (image.width > image.height) {//宽大于高时,按宽的比例缩
ImgD.width = width;
ImgD.height = (width / ImgD.width * ImgD.height);
} else {//高大于宽时,按高的比例缩
ImgD.height = height;
ImgD.width = (height / ImgD.height * ImgD.width);
}
}
}

