<img id="show"/>
顯示後還要形成縮略圖:
js:
function
setImgSize(imgID,maxWidth,maxHeight)
{
var img = document.images[imgID];
if(maxWidth < 1)
{
if(img.height > maxHeight)
{
img.height = maxHeight;
}
return true;
}
if(maxHeight < 1)
{
if(img.width > maxWidth)
{
img.width = maxWidth;
}
return true;
}
if(img.height > maxHeight || img.width > maxWidth)
{
if((img.height / maxHeight) > (img.width / maxWidth))
{
img.height = maxHeight;
}
else
{
img.width = maxWidth;
}
return true;
}
}
{
var img = document.images[imgID];
if(maxWidth < 1)
{
if(img.height > maxHeight)
{
img.height = maxHeight;
}
return true;
}
if(maxHeight < 1)
{
if(img.width > maxWidth)
{
img.width = maxWidth;
}
return true;
}
if(img.height > maxHeight || img.width > maxWidth)
{
if((img.height / maxHeight) > (img.width / maxWidth))
{
img.height = maxHeight;
}
else
{
img.width = maxWidth;
}
return true;
}
}
調用:
<script language="javascript" type="text/javascript" src="setImgSize.js"></script>
<img alt="" id="img1" onload="setImgSize('img1',150,0);" src=" http://img.poco.cn/photo/20060602/972374149620060602140117_1.jpg" />