图片自动按宽高比例缩小的js代码
分类:程序开发 Tags: 图片 javascript 评论:0 浏览:4052 最近更新时间: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判断身份证号码输入是否正确函数代码
• ASP保存远程图片文件到本地代码
• ASP按字段中首字母输出所有数据代码
• opencart 3.0 PayPal Payments Standard支付没有传递收货地址解决办法
• 去除新版宝塔面板首页的强制手机登录窗口教程
• 支付宝即时到账POST按钮代码
-
• 兼容ie,火狐,chrome,safari,360,qq,遨游,搜狗等浏览器的设为首页和加入收藏代码
• 2007年最具趣味的网站大全
• 通过css代码做Table表格隔行变色效果
• fckeditor报错FCKLang undefined(fcklang未定义)的解决方法
• 如何免费点亮、关闭QQ自由幻想图标?
• 十个增强英文外贸购物网站信任度的认证
• 11.11光棍节搞笑短信+光棍节搞笑QQ表情+光棍节的由来
• EASEUS Partition Master 5.0.1 Unlimited Edition无损磁盘分区调整工具教程
• 如何访问英文版google.com,而不跳转到google.com.hk
• JS、ASP、PHP自动检测浏览器语言并转到相关URL