图片自动按宽高比例缩小的js代码
分类:程序开发 Tags: 图片 javascript 评论:0 浏览:4192 最近更新时间: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/
-
• Fckeditor 2.6.3 ASP版增加检测上传文件是否为木马功能
• 兼容IE和firefox火狐浏览器的网页两侧对联广告代码
• 使用Aspjpeg组件,自动按宽高比例生成产品缩略图ASP代码
• 301重定向代码(ASP,ASP.NET,PHP,apache,IIS,ISAPI_Rewrite)
• ASP经常用到的代码,比较全,不可不看!
• ASP几种常见分页代码
• 1小时ASP入门,教你简单学会ASP
• ASP输出26个英文字母的方法代码
• asp执行出错,直接忽略错误代码
• ASP在后一页刷新前一页代码