ie6 fixed 很纠结~这个js就解决了

ie6真是个老大难的问题啊 前端人员心中永远的通

为了他一个浏览器 付出了我们多少代码啊~55

<script type="text/javascript">
      var position = function(){
          var isIE6 = !-[1,] && !window.XMLHttpRequest,
              html = document.getElementsByTagName('html')[0],
              dd = document.documentElement,
              db = document.body,
              dom = dd || db,
              // 获取滚动条位置
              getScroll = function(win){
                  return {
                      left: Math.max(dd.scrollLeft, db.scrollLeft),
                      top: Math.max(dd.scrollTop, db.scrollTop)
                      };
              };
    
          // 给IE6 fixed 提供一个"不抖动的环境"
          // 只需要 html 与 body 标签其一使用背景静止定位即可让IE6下滚动条拖动元素也不会抖动
          // 注意:IE6如果 body 已经设置了背景图像静止定位后还给 html 标签设置会让 body 设置的背景静止(fixed)失效
          if (isIE6 && document.body.currentStyle.backgroundAttachment !== 'fixed') {
              html.style.backgroundImage = 'url(about:blank)';
              html.style.backgroundAttachment = 'fixed';
          };
    
          return {
              fixed: isIE6 ? function(elem){
                  var style = elem.style,
                      doc = getScroll(),
                      dom = '(document.documentElement || document.body)',
                      left = parseInt(style.left) - doc.left,
                      top = parseInt(style.top) - doc.top;
                  this.absolute(elem);
                  style.setExpression('left', 'eval(' + dom + '.scrollLeft + ' + left + ') + "px"');
                  style.setExpression('top', 'eval(' + dom + '.scrollTop + ' + top + ') + "px"');
              } : function(elem){
                  elem.style.position = 'fixed';
              },
    
              absolute: isIE6 ? function(elem){
                  var style = elem.style;
                  style.position = 'absolute';
                  style.removeExpression('left');
                  style.removeExpression('top');
              } : function(elem){
                elem.style.position = 'absolute';
              }
          };
      }();
      </script>

//使用方法

var id = document.getElementById('id');

position.fixed(id);

position.absolute(id);

posted @ 2011-12-26 18:21  Nano  阅读(1350)  评论(1编辑  收藏  举报