有时候,我们需要临时或者不确定的加一些图片在我们的网页里面.
对于这些图片来说,浏览器会随机分配一些坐标给图片,或许这些参数
不适应图片,所以我们需要一个图片的自适应函数.
function autoImg(img,w,h)
{
if(w<=0 || h<=0)
return;
var image=new Image();
image.src=img.src;
if(image.width>w)
{
newHeight=image.height/image.width*w;
img.width=w;
img.height=newHeight;
}
if(img.height>h)
{
newWidth=image.width/image.height*h;
img.height=h;
img.width=newWidth;
}
}
相信这个函数对我们设计网页程序有点用吧!