JS实现超大图片按比例缩放
作者:shao65308 日期:2010-03-18
JS文件放在页面底部
<script type="text/javascript">
fAutoImgSizeX(698, 800);
function fAutoImgSizeX(iWidth, iHeight, sId, bExact, bNoReload)
{
var ImgEle, orange;
if(!sId)oRange = document.images;
else
{
if(!bExact) orange = document.getElementById(sId).getElementsByTagName('img'); else orange =[document.getElementById(sId)];
}
for(var i = 0; i < orange.length; i++ )
{
ImgEle = orange[i];
if(bNoReload)
{
var Result = fScaleSizeX(ImgEle.width, ImgEle.height,iWidth,iHeight);
ImgEle.width = Result.Width;
ImgEle.height = Result.Height;
}
else
{
var Temp = ImgEle.src;
ImgEle.onload =
function(e)
{
var ImgEle = this;
var Result = fScaleSizeX(ImgEle.width, ImgEle.height,iWidth,iHeight);
ImgEle.width = Result.Width;
ImgEle.height = Result.Height;
};
ImgEle.src = Temp;
}
}
}
function fScaleSizeX(iOldWidth, iOldHeight, iNewWidth, iNewHeight)
{
var Result = {};
if(iOldWidth > iNewWidth && (iOldHeight < iOldWidth))
{
Result.Width = iNewWidth;
Result.Height = iOldHeight - (iOldHeight/(iOldWidth/(iOldWidth - iNewWidth)));
}
else if(iOldHeight > iNewHeight && (iOldHeight > iOldWidth))
{
Result.Width = iOldWidth - (iOldWidth/(iOldHeight/(iOldHeight - iNewHeight)))
Result.Height = iNewHeight;
}
else
{
Result.Width = iOldWidth; Result.Height = iOldHeight;
}
return Result;
}
</script>
点击下载演示文件
程序代码
<script type="text/javascript">
fAutoImgSizeX(698, 800);
function fAutoImgSizeX(iWidth, iHeight, sId, bExact, bNoReload)
{
var ImgEle, orange;
if(!sId)oRange = document.images;
else
{
if(!bExact) orange = document.getElementById(sId).getElementsByTagName('img'); else orange =[document.getElementById(sId)];
}
for(var i = 0; i < orange.length; i++ )
{
ImgEle = orange[i];
if(bNoReload)
{
var Result = fScaleSizeX(ImgEle.width, ImgEle.height,iWidth,iHeight);
ImgEle.width = Result.Width;
ImgEle.height = Result.Height;
}
else
{
var Temp = ImgEle.src;
ImgEle.onload =
function(e)
{
var ImgEle = this;
var Result = fScaleSizeX(ImgEle.width, ImgEle.height,iWidth,iHeight);
ImgEle.width = Result.Width;
ImgEle.height = Result.Height;
};
ImgEle.src = Temp;
}
}
}
function fScaleSizeX(iOldWidth, iOldHeight, iNewWidth, iNewHeight)
{
var Result = {};
if(iOldWidth > iNewWidth && (iOldHeight < iOldWidth))
{
Result.Width = iNewWidth;
Result.Height = iOldHeight - (iOldHeight/(iOldWidth/(iOldWidth - iNewWidth)));
}
else if(iOldHeight > iNewHeight && (iOldHeight > iOldWidth))
{
Result.Width = iOldWidth - (iOldWidth/(iOldHeight/(iOldHeight - iNewHeight)))
Result.Height = iNewHeight;
}
else
{
Result.Width = iOldWidth; Result.Height = iOldHeight;
}
return Result;
}
</script>
评论: 0 | 引用: 0 | 查看次数: 3082
发表评论
你没有权限发表评论!