設定網頁最小最大寬度和高度(相容IE6)

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   java   for   

http://www.cnblogs.com/double-bin/archive/2011/12/19/2293093.html

 

/* 最小寬度 */

.min_width{min-width:300px;

  /* sets max-width for IE */

  _width:expression(document.body.clientWidth < 300 ? "300px" : "auto");

}

/* 最大寬度 */

.max_width{

  max-width:600px;

  /* sets max-width for IE */

  _width:expression(document.body.clientWidth > 600 ? "600px" : "auto");

}

/* 最小高度 */

.min_height{

  min-height:200px;

  /* sets min-height for IE */

  _height:expression(this.scrollHeight < 200 ? "200px" : "auto");

}

/* 最大高度 */

.max_height{

  max-height:400px;

  /* sets max-height for IE */

  _height:expression(this.scrollHeight > 400 ? "400px" : "auto");

} 

/* 最大最小寬度 */

.min_and_max_width{

  min-width:300px;

  max-width:600px;

  /* sets min-width & max-width for IE */

  _width: expression(

    document.body.clientWidth < 300 ? "300px" :

       ( document.body.clientWidth > 600 ? "600px" : "auto")

  );

} 

/* 最大最小高度 */

.min_and_max_height{

  min-height:200px;

  max-height:400px;

  /* sets min-height & max-height for IE */

  _height: expression(

    this.scrollHeight < 200 ? "200px" :

      ( this.scrollHeight > 400 ? "400px" : "auto")

  );

}

 另插一條限定圖片最大寬度和高度的代碼,僅供參考,其實與上述同源同理。

Html代碼  

這裡是結合css和其支援的expression實現的,範例程式碼如下:  
<title>控製圖片的最大高寬</title>  
  <style type="text/css">  
       .clear{clear:both;}  

.widthimg{width: expression(this.width > 200 ? ‘200px‘: true); max-width: 200px; }  
</style>    
<body>  
<div class="clear">  
<img class="widthimg" src="../image/mm.jpg" />  
</div>  
<div class="clear">  
<img src="../image/xx7.jpg" />  
</div>  
</body>  
 </html>  


 <head>  
<style type="text/css">     
<!--  
img.pic{     
max-width:300;  
max-height:100px;   
/*由於IE6.0以及以前版本的IE不支援上邊兩個屬性,所以加上以下兩條語句.這裡要說明的是expression只有IE支援*/    
width: expression(this.width > 300 && this.width / 300 >= this.height / 100 ? 300 : true);    
height: expression(this.height > 100 && this.width / 300 < this.height / 100 ? 100 : true);   
}   
//-->    
</style>  
 </head>   
<body>   
<div id="pic">    
</div>   
<img class="pic" src="Blue hills.jpg" />   
</body>   
</html>  
由於上邊的代碼中,設定最大的長寬是直接用數位,這樣再修改的時候十分不方便,所以做了以下修改:    
<html>   
<head>  
<script type="text/javascript">  
<!--  
Object.MAX_WIDTH = 400;  
Object.MAX_HEIGHT = 300;  
-->    
</script>   
<style type="text/css">    
<!--   
img.pic{    
max-width:300px;  
max-height:100px;  
width: expression(this.width > Object.MAX_WIDTH && this.width / Object.MAX_WIDTH >= this.height / Object.MAX_HEIGHT ? Object.MAX_WIDTH : true);   
height: expression(this.height > Object.MAX_HEIGHT && this.width / Object.MAX_WIDTH < this.height / Object.MAX_HEIGHT ? Object.MAX_HEIGHT : true);  
}    
//-->   
</style>  
</head>  
<body>   
<div id="pic">    
</div>   
<img class="pic" src="Blue hills.jpg" />   
</body>  
</html>  
這樣的話,只需要修改Object.MAX_WIDTH和Object.MAX_HEIGHT這兩個類變數就可以很方便的修改最大的長寬.

設定網頁最小最大寬度和高度(相容IE6)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.