js图片等比例缩放
2010-01-05 09:28
267 查看
<script language="JavaScript" type="text/javascript">
function DrawImage(ImgD,FitWidth,FitHeight)
{
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0)
{
if(image.width/image.height>= FitWidth/FitHeight)
{
if(image.width>FitWidth)
{
ImgD.width=FitWidth;
ImgD.height=(image.height*FitWidth)/image.width;
}
else
{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else
{
if(image.height>FitHeight)
{
ImgD.height=FitHeight;
ImgD.width=(image.width*FitHeight)/image.height;
}
else
{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
</script>
<img src="asfds.jpg" />
<img src="asfds.jpg" alt="自动缩放后的效果" onload="javascript:DrawImage(this,300,200);" />
原图:
效果图:
function DrawImage(ImgD,FitWidth,FitHeight)
{
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0)
{
if(image.width/image.height>= FitWidth/FitHeight)
{
if(image.width>FitWidth)
{
ImgD.width=FitWidth;
ImgD.height=(image.height*FitWidth)/image.width;
}
else
{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else
{
if(image.height>FitHeight)
{
ImgD.height=FitHeight;
ImgD.width=(image.width*FitHeight)/image.height;
}
else
{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
</script>
<img src="asfds.jpg" />
<img src="asfds.jpg" alt="自动缩放后的效果" onload="javascript:DrawImage(this,300,200);" />
原图:
效果图:
相关文章推荐
- 【JS控制图片显示的大小(图片等比例缩放)】
- js按比例缩放图片尺寸
- JS控制图片显示的大小(图片等比例缩放)
- 【js与jquery】js控制图片显示大小(图片等比例缩放)
- js 小小jquery等比例缩放图片效果
- JS控制图片显示的大小(图片等比例缩放)
- js等比例缩放图片
- JS控制图片显示的大小(图片等比例缩放)
- js实现图片的等比例缩放
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- js同比例缩放图片的小例子
- js同比例缩放图片的小例子
- js控制图片按比例缩放
- js 图片高度按比例缩放
- JS实现控制图片显示大小的方法【图片等比例缩放功能】
- JS控制图片等比例缩放的示例代码
- JS控制图片显示的大小(图片等比例缩放)
- [JS]JS控制图片超过宽度自动缩小图片等比例缩放源代码实例
- JS等比例缩放图片