两款js图片等比例缩放(兼容IE6,IE7,FF)
成功志
两款js图片等比例缩放(兼容IE6,IE7,FF)
2012-4-16 ok12

为了兼容多浏览器,先定义了一个高度与宽度,然后再利用js this.width,this.height进行判断再缩放图片。

<script language="网页特效" type="text/javascript">

 <!--

 function imgauto(i){//你必须给图片提前设定初始宽度和高度,建议直接就是最大宽度和高度

  var maxw=250; //定义图片显示的最大宽度

  var maxh=250; //定义图片显示的最大高度

  var o=new image();o.src=i.src;var w=o.width;var h=o.height;var t;if (w>maxw){t=maxw;}else{t=w;}

  if ((h*t/w)>maxh){i.height=maxh;i.width=maxh/h*w;}else{i.width=t;i.height=t/w*h;}

 }

  -->

 </script>

 <img src="/attachments/forumid_42/xp_iustd0jh5ssb.jpg" width="250" height="250" onload="imgauto(this)">




 代码二

<script language="javascript" type="text/javascript">

 <!--

 function imgauto(i,maxw,maxh){var o=new image();o.src=i.src;var w=o.width;var h=o.height;var t;if (w>maxw){t=maxw;}else{t=w;}if ((h*t/w)>maxh){i.height=maxh;i.width=maxh/h*w;}else{i.width=t;i.height=t/w*h;}}

  -->

 </script>

 <img src="/www.027eat.com/xp_iustd0jh5ssb.jpg" width="250" height="250" onload="imgauto(this,250,250)">

 <img src="/xp_iustd0jh5ssb.jpg" width="250" height="250" onload="imgauto(this,250,250)">


发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容