文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>IE6不识min-width三种解决方法

IE6不识min-width三种解决方法

时间:2008-02-08  来源:lucent918

第一种 在width后面加上expression(document.body.clientWidth <= 600? "600px": "auto" );
其中600px自行设置。
  width:expression(document.body.clientWidth <= 600? "600px": "auto" );
min-width:600px;
  第二种

min-width默认是不被IE6支持的,不清楚IE7的情况,在FF中支持很好,而且这也是一个非常有用的属性。

既然IE6默认不支持,我们肯定就要使用一些技巧来至少表面上看来是支持的。

思路:3个div相对定位嵌套,通过border-left,和margin-left取负值的办法,实现最小宽度为border-left的值。

1,定义此width宽度为90%,min-width为400px;rule为400px的盒子做对比,此时用FF能看到我们的预期效果,在IE下不行:

.width { width:90%; min-width:400px; border:1px solid #aaa;}
.rule { width:400px; background:#c00; border:1px solid #c00; color:#fff; margin:10px 0; text-align:center; padding:5px 0;}

<div class="width">
内容填充内容填充内容填充内容填充内容填充内容填充内容填充
</div>
<div class="rule">这是400px宽</div>

效果演示:http://www.rexsong.com/blog/attachments/200512/13_202952_minwidth1.htm

2,在width内嵌套了一层minwidth,定义border-left为400px,同时相对定位,看到左边框空白的效果,留出我们需要的位置。

.width { width:90%; min-width:400px; border:1px solid #aaa;}
.rule { width:400px; background:#c00; border:1px solid #c00; color:#fff; margin:10px 0; text-align:center; padding:5px 0;}
* html .minwidth { border-left:400px solid #fff; position:relative; float:left; z-index:1;}

<div class="width">
<div class="minwidth">内容填充内容填充内容填充内容填充内容填充内容填充</div>
</div>
<div class="rule">这是400px宽</div>

效果演示:http://www.rexsong.com/blog/attachments/200512/13_203000_minwidth2.htm

3,在minwidth内再嵌套一层container,定义负左边距等于minwidth层的border-left值,同时相对定位,完成效果。

.width { width:90%; min-width:400px; border:1px solid #aaa;}
.rule { width:400px; background:#c00; border:1px solid #c00; color:#fff; margin:10px 0; text-align:center; padding:5px 0;}
* html .minwidth { border-left:400px solid #fff; position:relative; float:left; z-index:1;}
* html .container { margin-left:-400px; position:relative; float:left; z-index:2; padding:3px;}

<div class="width">
<div class="minwidth">
<div class="container">
最内层必须要有内容填充,最内层必须要有内容填充,最内层必须要有内容填充,最内层必须要有内容填充,最内层必须要有内容填充。
</div>
</div>
</div>
<div class="rule">这是400px宽</div>

效果演示:http://www.rexsong.com/blog/attachments/200512/13_203009_minwidth3.htm

这又是一个负边距例子,我们不难理解:当container宽超过400px时,container右边距和minwidth一样自动延伸;当container宽不超过400px时,container右边距等于minwidth左边距,也就是说container的宽就是minwidth的border-left,只不过浮动在上边而已。

提示:* html .minwidth的写法是IE6专用(IE5未知,有兴趣的测试),可以使用其他hack手段。

基本思路而已,如果需要提高兼容性,可在此基础扩展。个人认为实用有限,因为使用了浮动,不过在某些场合表现出来还是蛮神奇的。

How to Use CSS to Solve min-width Problems in Internet Explorer http://www.webreference.com/programming/min-width/
无忧脚本

第三种

Javascript:

  1. // 说明:让 IE6 及其以下版本支持 CSS 中 min/max-width/height 属性
  2. // 整理:http://www.codebit.cn
  3. // minmax.js: make IE5+/Win support CSS min/max-width/height
  4. // version 1.0, 08-Aug-2003
  5. // written by Andrew Clover <[email protected]>, use freely
  6. /*@cc_on
  7. @if (@_win32 && @_jscript_version>4)
  8. var minmax_elements;
  9. minmax_props= new Array(
  10.    new Array('min-width', 'minWidth'),
  11.    new Array('max-width', 'maxWidth'),
  12.    new Array('min-height','minHeight'),
  13.    new Array('max-height','maxHeight')
  14. );
  15. // Binding. Called on all new elements. If <body>, initialise; check all
  16. // elements for minmax properties
  17. function minmax_bind(el) {
  18.    var i, em, ms;
  19.    var st= el.style, cs= el.currentStyle;
  20.    if (minmax_elements==window.undefined) {
  21.      // initialise when body element has turned up, but only on IE
  22.      if (!document.body || !document.body.currentStyle) return;
  23.      minmax_elements= new Array();
  24.      window.attachEvent('onresize', minmax_delayout);
  25.      // make font size listener
  26.      em= document.createElement('div');
  27.      em.setAttribute('id', 'minmax_em');
  28.      em.style.position= 'absolute'; em.style.visibility= 'hidden';
  29.      em.style.fontSize= 'xx-large'; em.style.height= '5em';
  30.      em.style.top='-5em'; em.style.left= '0';
  31.      if (em.style.setExpression) {
  32.        em.style.setExpression('width', 'minmax_checkFont()');
  33.        document.body.insertBefore(em, document.body.firstChild);
  34.      }
  35.    }
  36.    // transform hyphenated properties the browser has not caught to camelCase
  37.    for (i= minmax_props.length; i-->0;)
  38.      if (cs[minmax_props[i][0]])
  39.        st[minmax_props[i][1]]= cs[minmax_props[i][0]];
  40.    // add element with properties to list, store optimal size values
  41.    for (i= minmax_props.length; i-->0;) {
  42.      ms= cs[minmax_props[i][1]];
  43.      if (ms && ms!='auto' && ms!='none' && ms!='0' && ms!='') {
  44.        st.minmaxWidth= cs.width; st.minmaxHeight= cs.height;
  45.        minmax_elements[minmax_elements.length]= el;
  46.        // will need a layout later
  47.        minmax_delayout();
  48.        break;
  49.    } }
  50. }
  51. // check for font size changes
  52. var minmax_fontsize= 0;
  53. function minmax_checkFont() {
  54.    var fs= document.getElementById('minmax_em').offsetHeight;
  55.    if (minmax_fontsize!=fs && minmax_fontsize!=0)
  56.      minmax_delayout();
  57.    minmax_fontsize= fs;
  58.    return '5em';
  59. }
  60. // Layout. Called after window and font size-change. Go through elements we
  61. // picked out earlier and set their size to the minimum, maximum and optimum,
  62. // choosing whichever is appropriate
  63. // Request re-layout at next available moment
  64. var minmax_delaying= false;
  65. function minmax_delayout() {
  66.    if (minmax_delaying) return;
  67.    minmax_delaying= true;
  68.    window.setTimeout(minmax_layout, 0);
  69. }
  70. function minmax_stopdelaying() {
  71.    minmax_delaying= false;
  72. }
  73. function minmax_layout() {
  74.    window.setTimeout(minmax_stopdelaying, 100);
  75.    var i, el, st, cs, optimal, inrange;
  76.    for (i= minmax_elements.length; i-->0;) {
  77.      el= minmax_elements[i]; st= el.style; cs= el.currentStyle;
  78.      // horizontal size bounding
  79.      st.width= st.minmaxWidth; optimal= el.offsetWidth;
  80.      inrange= true;
  81.      if (inrange && cs.minWidth && cs.minWidth!='0' && cs.minWidth!='auto' && cs.minWidth!='') {
  82.        st.width= cs.minWidth;
  83.        inrange= (el.offsetWidth<optimal);
  84.      }
  85.      if (inrange && cs.maxWidth && cs.maxWidth!='none' && cs.maxWidth!='auto' && cs.maxWidth!='') {
  86.        st.width= cs.maxWidth;
  87.        inrange= (el.offsetWidth>optimal);
  88.      }
  89.      if (inrange) st.width= st.minmaxWidth;
  90.      // vertical size bounding
  91.      st.height= st.minmaxHeight; optimal= el.offsetHeight;
  92.      inrange= true;
  93.      if (inrange && cs.minHeight && cs.minHeight!='0' && cs.minHeight!='auto' && cs.minHeight!='') {
  94.        st.height= cs.minHeight;
  95.        inrange= (el.offsetHeight<optimal);
  96.      }
  97.      if (inrange && cs.maxHeight && cs.maxHeight!='none' && cs.maxHeight!='auto' && cs.maxHeight!='') {
  98.        st.height= cs.maxHeight;
  99.        inrange= (el.offsetHeight>optimal);
  100.      }
  101.      if (inrange) st.height= st.minmaxHeight;
  102.    }
  103. }
  104. // Scanning. Check document every so often until it has finished loading. Do
  105. // nothing until <body> arrives, then call main init. Pass any new elements
  106. // found on each scan to be bound    
  107. var minmax_SCANDELAY= 500;
  108. function minmax_scan() {
  109.    var el;
  110.    for (var i= 0; i<document.all.length; i++) {
  111.      el= document.all[i];
  112.      if (!el.minmax_bound) {
  113.        el.minmax_bound= true;
  114.        minmax_bind(el);
  115.    } }
  116. }
  117. var minmax_scanner;
  118. function minmax_stop() {
  119.    window.clearInterval(minmax_scanner);
  120.    minmax_scan();
  121. }
  122. minmax_scan();
  123. minmax_scanner= window.setInterval(minmax_scan, minmax_SCANDELAY);
  124. window.attachEvent('onload', minmax_stop);
  125. @end @*/



由于只有 IE6 及其以下版本不支持min/max-width/height 属性,因此,我们可以用下面的调用方式:

Code:


<!--[if lt IE 7]>

<![endif]-->


 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载