0712-2888027 189-8648-0214
微信公眾號(hào)

孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號(hào)

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

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

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

復(fù)制代碼
代碼如下:

<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代碼代碼如下

復(fù)制代碼
代碼如下:

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

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

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

復(fù)制代碼
代碼如下:

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

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

復(fù)制代碼
代碼如下:

.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);
}

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

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

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

復(fù)制代碼
代碼如下:

<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>

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

隨便看看頁(yè)面圖片強(qiáng)制等比縮放,發(fā)有修改前后對(duì)照?qǐng)D

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

為使大家一目了然,發(fā)有對(duì)照?qǐng)D,可以發(fā)現(xiàn)簡(jiǎn)單的改動(dòng)確實(shí)能夠美化頁(yè)面!

隨便看看頁(yè)面的圖片不會(huì)等比縮放,而是直接壓縮成100*100px,為此,簡(jiǎn)單改css進(jìn)行圖片強(qiáng)制等比縮放

實(shí)例看看就明白了!

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

 

 修改后圖片效果:

 

修改位置:network.css

修改內(nèi)容:
 

復(fù)制代碼
代碼如下:

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

直接替換為:

復(fù)制代碼
代碼如下:

#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實(shí)現(xiàn)圖片等比縮放兼容IE6/IE7/火狐/谷歌
代碼如下:

復(fù)制代碼
代碼如下:

<!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實(shí)現(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)容
熱點(diǎn)內(nèi)容
展開