在web开发中,经常会遇到图片显示问题,比如在首页轮播图片或者抓取缩略图,因原图片尺寸过大,这个时候就需要对图片进行缩放,为了保持图片宽度和高度比例不变,这就需要图片按照等比例来进行缩放。
纯CSS实现图片等比例缩放
CSS样式,定义宽度200px,高度经CSS条件判断后等比例缩放
.img1 {margin: auto;WIDTH: 200px;} .img1 img{max-width: 100%!important;HEIGHT: auto!important;width:expression(this.width > 200 ? "200px" : this.width)!important;}
<div class="img1"> <img src="huahai.png" alt="img"/> <img src="picsize.png" alt="img"/> </div>
CSS样式,定义高度200px,宽度经CSS条件判断后等比例缩放
.img2 {margin:10px auto;height: 200px;} .img2 img{max-height: 100%!important;width: auto!important;height:expression(this.height > 200 ? "200px" : this.height)!important;}
<div class="img2"> <img src="huahai.png" alt="img"/> <img src="picsize.png" alt="img"/> </div>
img标签属性,定义高度200px,宽度等比例缩放
<img src="huahai.png" alt="img" height="200px"/>
在线演示:点击预览
压缩包下载地址:立即下载(1.2M) 提取密码: gtfp
javascript自动按比例压缩显示图片
<script type="text/javascript"> function AutoResizeImage(maxWidth,maxHeight,objImg){ var img = new Image(); img.src = objImg.src; var hRatio; var wRatio; var Ratio = 1; var w = img.width; var h = img.height; wRatio = maxWidth / w; hRatio = maxHeight / h; if (maxWidth ==0 && maxHeight==0){ Ratio = 1; }else if (maxWidth==0){// if (hRatio<1) Ratio = hRatio; }else if (maxHeight==0){ if (wRatio<1) Ratio = wRatio; }else if (wRatio<1 || hRatio<1){ Ratio = (wRatio<=hRatio?wRatio:hRatio); } if (Ratio<1){ w = w * Ratio; h = h * Ratio; } objImg.height = h; objImg.width = w; } </script>
援引:http://blog.snsgou.com/post-397.html有改动
<center> 原图显示<br /> onload="AutoResizeImage(0,0,this)<br /> <img src="picsize.png" border="0" width="0" height="0" onload="AutoResizeImage(0,0,this)" alt="pic"/> <img src="huahai.png" border="0" width="0" height="0" onload="AutoResizeImage(0,0,this)" alt="pic"/><br/><br /> 1.按宽度250压缩,不限制高度 按比例压缩<br /> onload="AutoResizeImage(250,0,this)"<br /> <img src="picsize.png" border="0" width="0" height="0" onload="AutoResizeImage(250,0,this)" alt="250 X 374"/> <img src="huahai.png" border="0" width="0" height="0" onload="AutoResizeImage(250,0,this)" alt="250 X 374"/><br /><br /> 2.按高度250压缩,不限制宽度 按比例压缩<br /> onload="AutoResizeImage(0,250,this)"<br /> <img src="picsize.png" border="0" width="0" height="0" onload="AutoResizeImage(0,250,this)" alt="166 X 250"/> <img src="huahai.png" border="0" width="0" height="0" onload="AutoResizeImage(0,250,this)" alt="166 X 250"/><br /><br /> 3.按高度250宽度250 按比例压缩<br /> onload="AutoResizeImage(250,250,this)"<br /> <img src="picsize.png" border="0" width="0" height="0" onload="AutoResizeImage(250,250,this)" alt="200 X 300"/> <img src="huahai.png" border="0" width="0" height="0" onload="AutoResizeImage(250,250,this)" alt="200 X 300"/><br /><br /> 4.高宽不等比例压缩 (400 X 512),此时高度不变,会自动按高度的比例压缩。<br /> onload="AutoResizeImage(400,512,this)"<br /> <img src="picsize.png" border="0" width="0" height="0" onload="AutoResizeImage(400,512,this)" alt="341 X 512"/> <img src="huahai.png" border="0" width="0" height="0" onload="AutoResizeImage(400,512,this)" alt="341 X 512"/><br /><br /> 5.高宽不等比例压缩 (300 X 600),此时宽度不变,会自动按宽度的比例压缩。<br /> onload="AutoResizeImage(300,600,this)"<br /> <img src="picsize.png" border="0" width="0" height="0" onload="AutoResizeImage(300,600,this)" alt="300 X 449"/> <img src="huahai.png" border="0" width="0" height="0" onload="AutoResizeImage(300,600,this)" alt="300 X 449"/><br /><br /> 6.如果图片本来的高度和宽度小于压缩的最大高度和宽度,则不会拉大显示图片(按原图显示)<br /> 原图444 x 207,压缩为 500 x 600,将保持原图显示<br /> onload="AutoResizeImage(500,600,this)"<br /> <img src="picsize.png" border="0" width="0" height="0" onload="AutoResizeImage(500,600,this)" alt="444 X 207"/> <img src="huahai.png" border="0" width="0" height="0" onload="AutoResizeImage(500,600,this)" alt="444 X 207"/><br /><br /> <br/> <p>返回<a href="http://www.weisico.com/tech/298.html">图片等比例缩放</a>: <a href="http://www.weisico.com/tech/298.html">http://www.weisico.com/tech/298.html</a></p> </center>
在线演示:点击预览
压缩包下载地址:立即下载(1.2MB) 提取密码: gtfp