JS等比例缩放图片,限定最大宽度和最大高度
//图片按比例缩放 var flag=false; function DrawImage(ImgD,iwidth,iheight){ //参数(图片,允许的宽度,允许的高度) var image=new Image(); 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; } } }
<img src="images/toplogo.gif" onload="javascript:DrawImage(this,100,100)">
相关推荐
-
js验证方法isBoolean.js javascript
2019-1-8
-
发送验证码 javascript
2019-1-8
-
plupload.js上传到阿里云OSS(前端定义文件名) javascript
2019-1-8
-
JS常用常用正则表达式 javascript
2019-1-8
-
ajax方式验证登录系统 javascript
2019-1-8
-
js正则验证 javascript
2019-1-8
-
判断手机横屏和竖屏方向 javascript
2019-1-7
-
React 的事件测试代码 javascript
2019-1-7
-
输入身份证号码,subString截取出生日,并判断性别 javascript
2019-1-8
-
js日期相关方法 javascript
2019-1-8