js等比例缩放图片
2011-11-29 10:45
344 查看
<script language="JavaScript">
//图片按比例缩放
var flag=false;
function DrawImage(ImgD){
var image=new Image();
var iwidth = 80; //定义允许图片最大宽度
var iheight = 80; //定义允许图片最大高度
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
</script>
<img src=".." onload = "DrawImage(this)">
//图片按比例缩放
var flag=false;
function DrawImage(ImgD){
var image=new Image();
var iwidth = 80; //定义允许图片最大宽度
var iheight = 80; //定义允许图片最大高度
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
</script>
<img src=".." onload = "DrawImage(this)">
相关文章推荐
- JS控制图片等比例缩放
- js等比例缩放图片
- 等比例缩放图片 js
- 图片预加载并且自动等比例缩放的JS插件[转]
- JS控制图片显示的大小(图片等比例缩放)
- js控制图片按比例缩放
- JS控制图片显示的大小(图片等比例缩放)
- 【js与jquery】js控制图片显示大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- js 等比例缩放图片
- 【JS控制图片显示的大小(图片等比例缩放)】
- JS实现按比例缩放图片的方法(附C#版代码)
- 关于图片按比例自适应缩放的js代码
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片按比例缩放并不超过一定宽度和高度(居中对齐)
- 纯JS实现图片预览与等比例缩放和居中
- js按比例缩放图片尺寸
- JS等比例缩放图片
- JS控制图片显示的大小(图片等比例缩放)