js图片等比缩放

mac2022-06-30  23

<script language="JavaScript" type="text/javascript">  <!--  function DrawImage(ImgD,FitWidth,FitHeight){      var image=new Image();      image.src=ImgD.src;      if(image.width>0 && image.height>0){          if(image.width/image.height>= FitWidth/FitHeight){              if(image.width>FitWidth){                  ImgD.width=FitWidth;                  ImgD.height=(image.height*FitWidth)/image.width;              }else{                  ImgD.width=image.width;                  ImgD.height=image.height;              }          } else{              if(image.height>FitHeight){                  ImgD.height=FitHeight;                  ImgD.width=(image.width*FitHeight)/image.height;              }else{                  ImgD.width=image.width;                  ImgD.height=image.height;              }          }      }  }  //-->  </script>

<img src="XXXX" alt="自动缩放后的效果"  οnlοad="javascript:DrawImage(this,"200","200");" />图片外边框<table width='150' height='150' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #666666; display:block; width:150px; height:150px;padding:50px;'>  <tr>    <td width='300' height='68' align='center'> </td>  </tr></table>

 

 

 

 

放入head里面  <script  language="JavaScript">  <!--  var  flag=false;  function  DrawImage(ImgD){       var  image=new  Image();       image.src=ImgD.src;       if(image.width>0  &&  image.height>0){         flag=true;         if(image.width/image.height>=  164/112){           if(image.width>164){               ImgD.width=164;           ImgD.height=(image.height*164)/image.width;           }else{           ImgD.width=image.width;               ImgD.height=image.height;           }           ImgD.alt=image.width+"×"+image.height;           }         else{           if(image.height>112){               ImgD.height=112;           ImgD.width=(image.width*112)/image.height;                     }else{           ImgD.width=image.width;               ImgD.height=image.height;           }           ImgD.alt=image.width+"×"+image.height;           }         }  }    //-->  </script>   图片调用  <a  href="<%=trim(rs("picurl"))%>"  target="_blank"><img  src="<%=trim(rs("imgurl"))%>"  border="0"  width="164"  height="112"  οnlοad="javascript:DrawImage(this);"></a>   这样可以实现等比例缩放!

 

 

网页技术---不会被撑破的表格显示文章经常遇到图文混排的情况,有时图片太大或文字太长,表格就会被撑大,破坏了整体布局 可以用javescript+css的手段解决。 第一步:保证表格内文字自动换行用CSS定义一个类table {table-layout: fixed;word-wrap:break-word;} table-layout: fixed; 保证表格大小固定,不能被撑大.word-wrap:break-word; 保证表格内文字换行显示. 第二步:自动缩放图片用javascript在图片的IMG标签里加一句οnlοad=""javascript:if(this.width>宽度)this.width=宽度""这句会在图片加载的时候调用,只设置宽度或高度都是等比缩放.(注意,如果只用这一步来限制大小,在载入页面的时候表格必定会有个先被撑大然后缩小的过程。) 最后一个小问题,图片会填满右边(如果是左对齐的话),不好看.右边还是留一个边比较好.用CSS再定义一个类..overflow-hidden { overflow: hidden; width: 宽度;}调整宽度,使右边出现一个空白边。完成。

转载于:https://www.cnblogs.com/lidj/p/3173350.html

相关资源:js操作图片等比缩放及旋转
最新回复(0)