JS等比例缩放图片
2007-08-19 11:16
387 查看
有时网页很容易被大图片撑开,下面的小技巧可以用来解决这个问题:<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="XXXX" alt="自动缩放后的效果" onload="javascript:DrawImage(this,"200","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="XXXX" alt="自动缩放后的效果" onload="javascript:DrawImage(this,"200","200");" />
相关文章推荐
- js控制图片按比例缩放
- 【JS控制图片显示的大小(图片等比例缩放)】
- js图片等比例缩放
- js 小小jquery等比例缩放图片效果
- JS控制图片显示的大小(图片等比例缩放)
- js按比例缩放图片尺寸
- 【js与jquery】js控制图片显示大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- js实现图片的等比例缩放
- js 图片缩放(按比例)控制代码
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- js同比例缩放图片的小例子
- js同比例缩放图片的小例子
- js 图片高度按比例缩放
- JS实现控制图片显示大小的方法【图片等比例缩放功能】
- JS控制图片等比例缩放的示例代码
- JS控制图片显示的大小(图片等比例缩放)
- [JS]JS控制图片超过宽度自动缩小图片等比例缩放源代码实例