JS实现超大图片按比例缩放

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>

下载文件 点击下载演示文件


[本日志由 shao65308 于 2010-03-18 04:38 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: JS
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2908
发表评论
你没有权限发表评论!