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移动设备判断方法大全 javascript
2019-1-8
-
React 的事件测试代码 javascript
2019-1-7
-
js判断设备,跳转app应用、android市场或者AppStore javascript
2019-1-8
-
输入身份证号码,subString截取出生日,并判断性别 javascript
2019-1-8
-
js正则验证 javascript
2019-1-8
-
点击空白处关闭弹窗 [ JavaScript ] javascript
2019-1-7
-
判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备) javascript
2019-1-8
-
JS屏蔽鼠标右键菜单、复制粘贴、选中等 javascript
2019-1-8
-
网站动态加载JS脚本 javascript
2019-1-8
-
全选,全不选,反选(jquery终极版) javascript
2019-1-7