日期:2014-05-16 浏览次数:20376 次
<script language="javascript">
function init()
{
RsizeAllImageById("imgProductItem", 150, 150);
}
//将页面内所有指定id的图片按比例缩放
function RsizeAllImageById(id, W, H)
{
var imgs = document.getElementsByTagName("img");
for(var i=0; i<imgs.length; i++)
{
?? if(imgs[i].id == id)
?? {
??? ResizeImage(imgs[i], W, H);
?? }
}
}
function ResizeImage(imageDest, W, H)
{
//显示框宽度W,高度H
var image = new Image();
image.src = imageDest.src;
if(image.width>0 && image.height>0)
{
??? //比较纵横比
??? if(image.width/image.height >= W/H)//相对显示框:宽>高
??? {
???? if(image.width > W) //宽度大于显示框宽度W,应压缩高度
???? {
?????????????? imageDest.width = W;
?????????????? imageDest.height = (image.height*W)/image.width;??
????????????? }
???? else //宽度少于或等于显示框宽度W,图片完全显示
???? {
?????????????? imageDest.width = image.width;??????
?????????????? imageDest.height = image.height;??
????????????? }
??? }
??? else//同理
??? {
???? if(image.height > H)
???? {
?????????????? imageDest.height = H;
?????????????? imageDest.width = (image.width*H)/image.height;
????????????? }
???? else
???? {
?????????????? imageDest.width = image.width;
?????????????? imageDest.height = image.height;
????????????? }
??? }
}
}
</script>
调用<BODY?onload="javascript:init();">
?
<img style="cursor:pointer" height="150" width="150" id="imgProductItem" name="imgProductItem">
?
?
?
?
个人签名
-------------------------------------
?
图盾 淘宝保护 保护图片 图片防盗
<script type="text/javascript"></script>