js图片等比缩放

<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'>&nbsp;</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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值