javascript图片等比例缩放代码:图片的尺寸在初始的状态下往往不能够完美的适应网页的布局,这个时候就需要对图片进行缩放处理,当然不能够是无规则的进行缩放,否则可能出现图片变形现象,下面是一段能够对图片进行等比例缩放的实例代码。代码如下:<!DOCTYPE html&g ...
javascript图片等比例缩放代码:
图片的尺寸在初始的状态下往往不能够完美的适应网页的布局,这个时候就需要对图片进行缩放处理,当然不能够是无规则的进行缩放,否则可能出现图片变形现象,下面是一段能够对图片进行等比例缩放的实例代码。
代码如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.51texiao.cn/" /><title>图片等比例缩放代码-蚂蚁部落</title><script type="text/javascript">//参数(图片,允许的宽度,允许的高度) function DrawImage(ImgD,iwidth,iheight){ var image=new Image(); image.src='/images/loading.gif' data-original=ImgD.src; if(image.width>0&& image.height>0){ 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; } } else{ if(image.height>iheight){ ImgD.height=iheight; ImgD.width=(image.width*iheight)/image.height; } else{ ImgD.width=image.width; ImgD.height=image.height; } } } } window.onload=function(){ var theimage=document.getElementById("theimage"); DrawImage(theimage,80,80)}</script></head><body><img src='/images/loading.gif' data-original="theimg.jpg" id="theimage"/></body></html>
原标题:javascript图片等比例缩放代码
关键词:JavaScript
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。