zoukankan      html  css  js  c++  java
  • 《转载》解决IE6 postion fixed 定位问题

    关于 position:fixed; 属性

    生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。

    position:fixed; 可以让网页上的某个元素固定在一个绝对的位置,即使拉动滚动条位置也不发生变化。(在 LOO2K 博客右下角的那个置顶的小按钮就是用了这个 CSS 属性实现的)

    一般的 position:fixed; 实现方法

    以我的博客为例,在右下角<div id="top">...</div>这个 HTML 元素使用的 CSS 代码如下:

    1. #top{
    2. position:fixed;
    3. bottom:0;
    4. right:20px;
    5. }

    实现让<div id="top">...</div>元素固定在浏览器的底部和距离右边的20个像素。

    在 IE6 中实现 position:fixed; 的办法

    刚刚提过,在 IE6 中是不能直接使用 position:fixed; 。你需要一些 CSS Hack 来解决它。(当然,IE6 的问题也不仅仅 position:fixed;)

    相同的还是让 <div id="top">...</div> 元素固定在浏览器的底部和距离右边的20个像素,这次的代码是:

    1. #top{
    2. position:fixed;
    3. _position:absolute;
    4. bottom:0;
    5. right:20px;
    6. _bottom:auto;
    7. _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));
    8. }

    right 跟 left 属性可以用绝对定位的办法解决,而 top 跟 bottom 就需要用上面的表达式来实现。其中在_position:absolute;中的_符号只有 IE6 才能识别,目的是为了区分其他浏览器

    上面的只是一个例子,下面的才是最重要的代码片段:

    使元素固定在浏览器的顶部

    1. #top{
    2. _position:absolute;
    3. _bottom:auto;
    4. _top:expression(eval(document.documentElement.scrollTop));
    5. }

    使元素固定在浏览器的底部

    1. #top{
    2. _position:absolute;
    3. _bottom:auto;
    4. _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));
    5. }

    这两段代码只能实现在最底部跟最顶部,你可以使用 _margin-top:10px; 或者 _margin-bottom:10px; 修改其中的数值控制元素的位置。

    (纠正,以上是网络转载的。这里如果要想修改_top的属性,就是不定位在最底部或者不是在最顶部也有办法。比如是距离顶部300px我就是这样做的:_top:expression(eval(document.documentElement.scrollTop+300));这个可以实现,测试通过)

    position:fixed; 闪动问题

    现在,问题还没有完全解决。在用了上面的办法后,你会发现:被固定定位的元素在滚动滚动条的时候会闪动。解决闪动问题的办法是在 CSS 文件中加入:

    1. *html{
    2. background-image:url(about:blank);
    3. background-attachment:fixed;
    4. }

    其中 * 是给 IE6 识别的。

    到此,IE6 的 position:fixed; 问题已经被解决了。现在 LOO2K 这个博客上的固定定位就是使用的这个办法解决 IE6 固定定位问题的。

    原帖地址:http://hi.baidu.com/touchiyudeji/item/7c4024f16da34ec1a935a25c

  • 相关阅读:
    Yarn架构基本概况(二)
    Yarn架构基本概况(二)
    Yarn架构基本概况(二)
    Yarn架构基本概况(一)
    Yarn架构基本概况(一)
    Yarn架构基本概况(一)
    从Hadoop 安全机制原理到当今主流安全机制
    WebService数据示例
    CXF浅析
    WebService的网络协议
  • 原文地址:https://www.cnblogs.com/BiakeChou/p/2562823.html
Copyright © 2011-2022 走看看