图片自动按宽高比例缩小的js代码
分类:程序开发 Tags: 图片 javascript 评论:0 浏览:4084 最近更新时间:2010/7/16 13:24:00
- <script type="text/javascript">
- function setImgSize(img,width,height){
- var MaxWidth=width;//设置图片宽度界限
- var MaxHeight=height;//设置图片高度界限
- var HeightWidth=img.offsetHeight/img.offsetWidth;//设置高宽比
- var WidthHeight=img.offsetWidth/img.offsetHeight;//设置宽高比
- if(img.offsetWidth>MaxWidth){
- img.width=MaxWidth;
- img.height=MaxWidth*HeightWidth;
- }
- if(img.offsetHeight>MaxHeight){
- img.height=MaxHeight;
- img.width=MaxHeight*WidthHeight;
- }
- }
- </script>
- <img src="/pic/images4/20100716232307734.gif" border=0 onload="setImgSize(this,150,150);">
- //按比例缩小图片
- //调用方式onload="setImgSize(this,150,150)"
- var flag=false;
- function setImgSize(ImgD,Imgwidth,Imgheight){
- var image=new Image();
- image.src=ImgD.src;
- if(image.width>0 && image.height>0){
- flag=true;
- if(image.width/image.height>= 1){
- if(image.width>Imgwidth){
- ImgD.width=Imgwidth;
- ImgD.height=(image.height*Imgwidth)/image.width;
- }else{
- ImgD.width=image.width;
- ImgD.height=image.height;
- }
- }
- else{
- if(image.height>Imgheight){
- ImgD.height=Imgheight;
- ImgD.width=(image.width*Imgheight)/image.height;
- }else{
- ImgD.width=image.width;
- ImgD.height=image.height;
- }
- }
- }
- }
本文地址:/478/
-
• asp中判断一个字符是不是汉字
• 一个空间/虚拟主机放置多个网站的转向代码(多个域名绑定一个空间)
• 解决IE6下float浮动margin出现双倍距离问题
• ASP判断身份证号码输入是否正确函数代码
• ASP保存远程图片文件到本地代码
• 去除新版宝塔面板首页的强制手机登录窗口教程
• win2003空间支持access2007、2010数据库.accdb的方法
• ASP经常用到的代码,比较全,不可不看!
• IE6中使用first-letter首字符css的特殊写法
• 获取文件的名称和扩展名ASP代码