search
HomeWeb Front-endHTML TutorialLet IE6 support the position:fixed method, CSS expression and JavaScript eval explained_html/css_WEB-ITnose

When making ceiling effects or fixed effects, it is most convenient to use position:fixed, but the evil IE6 does not have the attribute value fixed, and we want to make IE6 behave like fixed Fixing it at a certain position in the browser and using onscroll to change the top value is one method, but if the wheel scrolls too fast, flickering will occur. If an advanced browser also uses it this way, it would obviously be inelegant, unless the browser version is determined in JS. But what I want to explain here is the use of CSS to achieve the fixed effect.
Thousands of words are not as good as one example:

The above is the author’s fix for testing IE6 under IETester. Pay attention to the scroll bar. The navigation uses position:fixed.
The core code is as follows:

1 .nav {    /* nav为导航栏 */2     position:fixed;3     _position: absolute;4     top:0;5     _top:expression(eval(documentElement.scrollTop));6     background: #FAA;7 }

Using hack technology, the attributes represented by the underline are only recognized by IE6 and below. (Because only IE6 and below do not support fixed, so special processing)
The above effect can achieve the fixed effect under IE6. It is not easy to understand _top:expression(eval(documentElement.scrollTop)); you need to understand the meaning and usage of expression and eval, which is what will be explained below.

Expression is a unique attribute of IE. It supports IE5 and above and is used to write JavaScript code in CSS. That is to say, the brackets of expression can be a piece of JavaScript code.

eval means executing a statement or expression of the string content and returning its result.


Usage: eval(codes);

Parameters:

  • codes -- expression or statement in string form
  • Return value:

  • If there are no parameters, return undefined
  • If there is a return value, this value will be returned, otherwise undefined
  • If it is an expression, return the value of the expression
  • If the value of the statement is returned for a statement
  • If the value of the last statement is returned for multiple statements or expressions

  • After understanding expression and eval, _top:expression(eval( documentElement.scrollTop)); is not difficult to understand. documentElement.scrollTop obtains the position of the scroll bar under IE, and the top value is equivalent to the position of the scroll bar from the top. If the scrollTop value changes, _top changes accordingly.
    And why use eval? Because documentElement.scrollTop is actually a statement and has no return value, which is equivalent to executing a=1 in JS without performing any operations. If we want to get the documentElement.scrollTop value, we need to return it, and we only need to use eval.
    In this way, all browsers can have a fixed effect, but you will find that flickering will still occur when scrolling the mouse wheel under IE6. This is because when the scroll bar scrolls or the browser size changes, IE6 The weird rendering engine resets everything and redraws the page, so you get vibration or flickering issues. Using backgroune-attachment:fixed; added to html or body will force the CSS to be loaded before the page is redrawn, because the CSS processed before redrawing, that is, your expression has been changed before redrawing, unlike the previous redrawing. Change only after painting. This way there will be no flickering when your mouse scrolls. This completely achieves the fixed effect. The code is as follows:
    body { _background: url(about: blank) fixed}

    Summary:
    fixed cannot be implemented under IE6, so absolute is used to simulate fixed, so JS needs to be used. For processing only for IE6, JavaScript can be written in CSS through IE's unique expresstion, thereby changing the top value in real time to simulate a fixed effect. However, IE6 reloads CSS when scrolling or resizing the browser, so use background-attachment:fixed in the body to load CSS before re-rendering the page.

    Code:

     1 body {_background: url(about:blank) fixed} 2  3 .nav { 4  5     position: fixed; 6  7     _position: absolute; 8  9     top: 0;10 11     _top: expression(eval(documentElement.scrollTop));    12 13     // top值为想要固定定位的位置,设置left同理,设置right与bottom需要通过scrollLeft及scrollTop进行计算,如下注释14 15     // 固定左:_left:expression(eval(documentElement.scrollLeft));16 17     // 固定右:_left:expression(eval(documentElement.scrollLeft + documentElement.clientWidth - this.offsetWidth));18 19     // 固定下:_top:expression(eval(documentElement.scrollTop + documentElement.clientHeight - this.offsetHeight));20 21 }

    The above is the perfect solution to the fixed method under IE6. If you have any questions or concerns please leave a message.

    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
    The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

    The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

    Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

    HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

    What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

    The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

    HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

    How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

    Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

    What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

    HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

    How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

    TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

    HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

    HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Hot Tools

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor