利用CSS按比例缩小图片

把一副大图片按比例缩小到某个尺寸,对于现代浏览器,直接使用max-width和max-height两条CSS属性即可。阅读全文...

对于IE 6.0及以下版本,以上两条CSS属性均不会被理会。之前处理这种事情,我们往往会借助Javascript,然后为图片加上onload事件。例如:
引用内容 引用内容
<img src="..." alt="..." onload="resizeImage(this)" />
<script type="text/javascript">
function resizeImage(obj) {
    obj.width = obj.width > 50 && obj.width > obj.height ? 50 : auto;
    obj.height = obj.height > 50 ? 50 : auto;
}
</script>


这固然能解决问题,但是对以后页面的升级会带来麻烦——随着浏览器对CSS支持的完善,我们迟早会把图片上的onload事件统统去除。该是Expression的Show Time了,既然IE支持通过Expression在CSS中放置一些脚本,而这段脚本又只是提供给IE 6.0及以下版本使用,那么把它写到Expression中再合适不过。

最终,把一副大图片按比例缩小到50px*50px以内,可以参照以下这段CSS:
引用内容 引用内容
.thumbImage {
    max-width: 50px;
    max-height: 50px;
}
* html .thumbImage {
    width: expression(this.width > 50 && this.width > this.height ? 50 : auto);
    height: expresion(this.height > 50 ? 50 : auto);
}


至于图片是如何保持其高宽比例的,这张图片可以解释:
点击查看图片[haao.cn]
<script type="text/javascript"><!--
google_ad_client 
= "pub-7655768891627260";
google_ad_width 
= 728;
google_ad_height 
= 90;
google_ad_format 
= "728x90_as";
google_ad_type 
= "text_image";
//2007-07-16: 酷哥广告渠道
google_ad_channel = "5559849160";
google_ui_features 
= "rc:0";
//-->
</script>
<script type="text/javascript"
  src
="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
 
posted @ 2007-07-16 22:55  hibernate3例子  阅读(167)  评论(0编辑  收藏  举报