0712-2888027 189-8648-0214
微信公眾號

孝感風信網(wǎng)絡科技有限公司微信公眾號

當前位置:主頁 > 技術(shù)支持 > HTML5/CSS3 > css控制圖片按等比例縮放實例兼容IE6/IE7/火狐/谷歌

css控制圖片按等比例縮放實例兼容IE6/IE7/火狐/谷歌

時間:2024-09-14來源:風信官網(wǎng) 點擊: 1766次
昨天自己的一個網(wǎng)站需要實現(xiàn)圖片等比例縮放,但又不能使用js只能用css控制div中的img為指定大小,下面我找到一段代碼分離給大家。  

復制代碼
代碼如下:

<style type="text/css">
.thumbImage img{
MAX-WIDTH: 100%!important;HEIGHT: auto!important;width:expression(this.width > 600 ? "600px" : this.width)!important;
}
.thumbImage {MARGIN: auto;WIDTH: 600px;}
*html.thumbImage img{
width:expression(this.width>600&&this.width>this.height?450:auto);
height:expresion(this.height>450?450:auto);
}
</style>

html代碼代碼如下

復制代碼
代碼如下:

<body>
<DIV class="thumbImage" ><img src="2007910112041114.jpg" border="0"></DIV>
</body>

這樣只要在 class中thumbImage圖片都會等比例縮放哦,小了就原始顯示大的就等比例縮小。

按比例縮小或者放大到某個尺寸,對于標準瀏覽器(如Firefox),或者最新都IE7,ie8瀏覽器,
直接使用max-width,max-height;或者min-width,min-height的CSS屬性即可。如:
 

復制代碼
代碼如下:

img{max-width:100px;max-height:100px;}
img{min-width:100px;min-height:100px;}

對于IE6及其以下版本的瀏覽器,則可以利用其支持的expression屬性,在css code中嵌入javascript code來實現(xiàn)圖片的縮放,如下:

復制代碼
代碼如下:

.thumbImage {max-width: 100px;max-height: 100px;} /* for Firefox & IE7 */
* html .thumbImage { /* for IE6 */
width: expression(this.width > 100 && this.width > this.height ? 100 : auto);
height: expression(this.height > 100 ? 100 : auto);
}

首頁產(chǎn)品展示的圖片都變形了,客戶要求圖片等比例大小,和產(chǎn)品展示的效果一樣:

找到的相關(guān)知識:

css控制圖片的等比縮放
css樣式代碼
 

復制代碼
代碼如下:

<style type="text/css">
img {max-width:500px; max-height:500px; scale:expression((this.offsetWidth > this.offsetHeight)?(this.style.width = this.offsetWidth >= 500 ? "500px" : "auto"):(this.style.height = this.offsetHeight >= 500 ? "500px" : "auto")); display:inline !important;}
</style>

示例中是默認網(wǎng)頁所有的img的標簽中的圖片都會縮放,如果想讓特定的圖片縮放只要修改下前面的css名字,然后在網(wǎng)頁中調(diào)用就可以了

隨便看看頁面圖片強制等比縮放,發(fā)有修改前后對照圖

不是專業(yè)人員,沒有利用較高難度寫JS或者css,只是為了網(wǎng)站美觀,把網(wǎng)站版面美化的一些經(jīng)驗分享一下??!

為使大家一目了然,發(fā)有對照圖,可以發(fā)現(xiàn)簡單的改動確實能夠美化頁面!

隨便看看頁面的圖片不會等比縮放,而是直接壓縮成100*100px,為此,簡單改css進行圖片強制等比縮放

實例看看就明白了!

原版HOME隨便看看圖片效果:

 

 修改后圖片效果:

 

修改位置:network.css

修改內(nèi)容:
 

復制代碼
代碼如下:

#spics .spic_img img { width: 100px; height: 100px; }

直接替換為:

復制代碼
代碼如下:

#spics .spic_img img { max-width: 100px; max-height: 100px; zoom: expression( function(elm) { if (elm.width > 100 || elm.height > 100 ){ if (elm.width>elm.height) { elm.width=100; } else{ elm.height=100; } } elm.style.zoom = '1'; }(this) ); }

純CSS實現(xiàn)圖片等比縮放兼容IE6/IE7/火狐/谷歌
代碼如下:

復制代碼
代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>純CSS實現(xiàn)圖片等比縮放兼容IE6/IE7/火狐/谷歌_懶人建站</title>
<style type="text/css">
.suofang {MARGIN: auto;WIDTH: 800px;}
.suofang img{MAX-WIDTH: 100%!important;HEIGHT: auto!important;width:expression(this.width > 800 ? "800px" : this.width)!important;}
</style>
</head>
<body>
<div class="suofang">
<img src="http://www.51xuediannao.com/uploads/jiqiao/csssuofang/blogbus.png"/>
</div>
</body>
</html>
熱門關(guān)鍵詞: css 控制圖片 等比例縮放 IE6 IE7 火狐 谷歌
欄目列表
推薦內(nèi)容
熱點內(nèi)容
展開