Home  >  Article  >  Web Front-end  >  Detailed explanation of position:fixed problem in IE6

Detailed explanation of position:fixed problem in IE6

亚连
亚连Original
2018-05-21 15:21:202062browse

This article mainly introduces relevant information on the detailed explanation of the position:fixed problem and the effect of scrolling with the scroll bar in IE6. I hope this article can help everyone. Friends in need can refer to it

Detailed explanation of the position: fixed problem in IE6 and the effect of scrolling with the scroll bar

Foreword:

In "[jQuery] IE6 compatible scrolling monitoring" (Click to open the link) It is mentioned that to solve the IE6 fixed problem, specifically, you need to introduce a js file and declare a script to declare the fixed positioning for this p to solve it. This is not good at first. In addition to the difficulty of managing the imported Javascript, you also have to introduce JavaScript in the head statement, and then declare an id for the p, and then make a statement in the script. It is really annoying.

Using position: fixed is nothing more than trying to achieve the following effect.

Basically position: fixed is no problem in all browsers above IE7:

IE8:

Yehu Zen FireFox :

However, since there is no position: fixed attribute directly in IE6, the following effect must be made:

It can only be solved by using position: absolute; and adding a javascript script executed in the css style.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Untitled Document</title> 
    <style type="text/css">     
      .fixedbox { 
        background: #69C; 
        height: 60px; 
        width: 100px;         
        position: fixed; 
        bottom: 100px; 
        /*IE6实现position: fixed;*/ 
        /*等价于position: fixed;虽然代码好长,但是根本就不用管*/ 
        _position: absolute;         
        _top: expression(eval( 
        document.documentElement.scrollTop + document.documentElement.clientHeight-this.offsetHeight- 
        (parseInt(this.currentStyle.marginTop,10)||0)- 
        (parseInt(this.currentStyle.marginBottom,10)||0))); 
        /*等价于position: fixed;虽然代码好长,但是根本就不用管*/ 
        _margin-bottom:100px;/*设置位置,不要用bottom,而是改用margin-bottom,margin-xx来实现*/ 
      } 
    </style> 
  </head> 
  <body> 
    <p style="float:left;width:80%;min-height:100px;"> 
      <p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p> 
      <p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p> 
      <p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p> 
      <p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p><p>sss</p>    
    </p> 
    <p style="float:left;width:20%;min-height:100px;"><p class="fixedbox"></p></p> 
    <p style="clear:both"></p>     
  </body> 
</html>

The above code, for IE6 styles, is preceded by _, and the _ part is the IE6-specific rewritten style statement. For details, see "[CSS] About !important, *, and _Symbol》(Click to open the link)

In fact, in IE6, the following CSS:

.fixed{ 
  position: absolute;        
  top: expression(eval( 
    document.documentElement.scrollTop + document.documentElement.clientHeight-this.offsetHeight- 
    (parseInt(this.currentStyle.marginTop,10)||0)- 
    (parseInt(this.currentStyle.marginBottom,10)||0))); 
}

is equivalent to that of other browsers:

.fixed{ 
  position: fixed; 
}

Of course IE6 The CSS that implements position: fixed may not work properly in some browsers, so add an underscore _ in front of each style to indicate that it will only be executed in IE6.

At the same time, after IE6 should have the above style, do not use right, top, left, bottom to position like other browsers, but use margin-bottom, margin-left, margin-right to set the position: fixed The position of p,

You also need to pay attention when adjusting the position of p here. Since the above-mentioned CSS compatible with IE6 uses the top attribute, setting margin-top will not work. If you want to set this p When floating, how far away from the top of the browser is, you should write like this:

.fixed{ 
  /*IE6实现position: fixed;*/ 
  _position: absolute;         
  _top: expression(eval(document.documentElement.scrollTop)); 
  _margin-top:100px; 
}

The reason why the code about _top here is so much shorter is because there is no need to use document.documentElement.clientHeight to get it Browser display window size.

And -this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0) is all for fine-tuning to be more precise, You don’t have to add it if you don’t want it, it’s just a little visual effect.

Furthermore, as you can see from the above code, I did not set the right and left of the fixedbox because I want it to remain stable when scrolling with the scroll bar. The float:left attribute of parent p.

That is, the blue color block on the right and a lot of sss on the left are still divided into 80% and 20%.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

JavaScript memory leak problem in versions before IE9 (detailed summary)

JavaScript hasOwnProperty() function (picture Text tutorial, with code examples)

Differences and application scenarios of JS frameworks such as JQuery, Extjs, YUI, Prototype, Dojo (practical skills)

The above is the detailed content of Detailed explanation of position:fixed problem in IE6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn