Heim  >  Artikel  >  Web-Frontend  >  图片自动缩小的js代码,用以防止图片撑破页面_javascript技巧

图片自动缩小的js代码,用以防止图片撑破页面_javascript技巧

WBOY
WBOYOriginal
2016-05-16 19:16:511066Durchsuche

JS代码(随便放哪里):
<script> <BR><!-- <BR>var flag=false; <BR>function DrawImage(ImgD){ <BR>var image=new Image(); <BR>image.src=http://www.jb51.net/htmldata/2006-01-08/ImgD.src; <BR>if(image.width>0 && image.height>0){ <BR> flag=true; <BR> if(image.width/image.height>= 180/110){ <BR> if(image.width>180){ <BR> ImgD.width=180; <BR> ImgD.height=(image.height*110)/image.width; <BR> }else{ <BR> ImgD.width=image.width; <BR> ImgD.height=image.height; <BR> } <BR> /*ImgD.alt="bigpic" */ <BR> } <BR> else{ <BR> if(image.height>110){ <BR> ImgD.height=110; <BR> ImgD.width=(image.width*110)/image.height; <BR> }else{ <BR> ImgD.width=image.width; <BR> ImgD.height=image.height; <BR> } <BR> /*ImgD.alt="bigpic" */ <BR> } <BR>} <BR>} <BR>//--> <BR></script>
图片使用的地方:
图片自动缩小的js代码,用以防止图片撑破页面_javascript技巧
width="180"  style="max-width:90%"  注意这里最好限定,如果不限定加载图时会成原大,然后再缩小,这个过程如果图大了很难看的.这里是宽度和高度,在前面的JS里改,这里也作相应的改.
图不会变形,只会按比列缩,放心

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn