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 <and@doxdesk.com>, 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 @*/

 

 

 

 

<!--[if lt IE 7]>
<script type="text/javascript" src="minmax.js"></script>
<![endif]
-->


 

posted on 2009-11-14 11:35  nidilzhang  阅读(1029)  评论(0编辑  收藏  举报