js按比例缩放图片
2010-12-02 09:15
246 查看
//使用onload=DrawImage(this,长,宽);
function DrawImage(ImgD,w,h){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= w/h){
if(image.width>w){
ImgD.width=w;
ImgD.height=(image.height*w)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>h){
ImgD.height=h;
ImgD.width=(image.width*h)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"×"+image.height;
}
}
}
调用方法:
1、当然还是先在要用的页面调用这个JS
<script src="/js/img.js"></script>
2、在要缩略的图片上加上 onload=DrawImage(this,长,宽)
例如:<img src="#" onload="DrawImage(this,60,60)"
function DrawImage(ImgD,w,h){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= w/h){
if(image.width>w){
ImgD.width=w;
ImgD.height=(image.height*w)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>h){
ImgD.height=h;
ImgD.width=(image.width*h)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"×"+image.height;
}
}
}
调用方法:
1、当然还是先在要用的页面调用这个JS
<script src="/js/img.js"></script>
2、在要缩略的图片上加上 onload=DrawImage(this,长,宽)
例如:<img src="#" onload="DrawImage(this,60,60)"
相关文章推荐
- 按比例缩放图片的两种方法:JS&CSS
- 通过Js自动按比例缩放图片
- JS实现按比例缩放图片的方法(附C#版代码)
- js图片等比例缩放
- JS控制图片显示的大小(图片等比例缩放)
- 手机网站中 限制图片宽度 JS图片等比例缩放
- JS等比例缩放图片并构建图片水平垂直居中解决方案
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- js将图片按比例缩放显示IE6
- JS实现按比例缩放图片的方法(附C#版代码)
- js控制图片按比例缩放
- js等比例缩放图片
- js+jQuery按比例缩放图片(网络图片或本站图片均适用)
- 按比例缩放图片的js代码
- JS控制图片等比例缩放
- JS控制图片显示的大小(图片等比例缩放)
- JS网页图片按比例自适应缩放实现方法
- 等比例缩放图片 js
- 图片预加载并且自动等比例缩放的JS插件[转]