zoukankan      html  css  js  c++  java
  • 让IE6也支持position:fixed

    如何让position:fixed在IE6中工作呢?

    本文所使用的技巧是用了一条InternetExplorer的CSS表达式(expression)。你不可以直接使用该表达式,因为它可能会因为缓存而不更新。解决这一点的最简单的方式是使用eval包裹你的语句。

    如何解决抖动的问题?

    显然IE有一个多步的渲染进程。当你滚动或调整你的浏览器大小的时候,它将重置所有内容并重画页面,这个时候它就会重新处理css表达式。这会引起一个丑陋的“抖动”bug,在此处固定位置的元素需要调整以跟上你的(页面的)滚动,于是就会“跳动”。

    解决此问题的技巧就是使用background-attachment:fixed为body或html元素添加一个background-image。这就会强制页面在重画之前先处理CSS。因为是在重画之前处理CSS,它也就会同样在重画之前首先处理你的CSS表达式。这将让你实现完美的平滑的固定位置元素!

    我发现的另外一个小技巧是,你根本无需一个真实的图片!你可以使用一个about:blank替代一个spacer.gif图片,而且它工作的同样出色。

    /*让position:fixed在IE6下可用!*/
    02 .fixed-top/*头部固定*/{position:fixed;bottom:auto;top:0px;}
    03 .fixed-bottom/*底部固定*/{position:fixed;bottom:0px;top:auto;}
    04 .fixed-left/*左侧固定*/{position:fixed;right:auto;left:0px;}
    05 .fixed-right/*右侧固定*/{position:fixed;right:0px;left:auto;}
    06  
    07 /*上面的是除了IE6的主流浏览器通用的方法*/
    08 *html,*htmlbody/*修正IE6振动bug*/
    09 {background-image:url(about:blank);background-attachment:fixed;}
    10 *html.fixed-top/*IE6头部固定*/
    11 {position:absolute;bottom:auto;
    12 top:expression(eval(document.documentElement.scrollTop));}
    13 *html.fixed-right/*IE6右侧固定*/
    14 {position:absolute;right:auto;
    15 left:expression(eval(document.documentElement.scrollLeft
    16 +document.documentElement.clientWidth-this.offsetWidth)
    17 -(parseInt(this.currentStyle.marginLeft,10)||0)
    18 -(parseInt(this.currentStyle.marginRight,10)||0));}
    19  
    20 *html.fixed-bottom/*IE6底部固定*/
    21 {position:absolute;bottom:auto;
    22 top:expression(eval(document.documentElement.scrollTop
    23 +document.documentElement.clientHeight-this.offsetHeight
    24 -(parseInt(this.currentStyle.marginTop,10)||0)
    25 -(parseInt(this.currentStyle.marginBottom,10)||0)));}
    26 *html.fixed-left/*IE6左侧固定*/
    27 {position:absolute;right:auto;
    28 left:expression(eval(document.documentElement.scrollLeft));}

    更新:添加border、padding和margin支持!

    如果你不需要支持margin,可以将所有的`parseInt`部分去掉。以上只在标准模式下进行了测试。

  • 相关阅读:
    【Matlab】把一年中的某一天(从1月1日起)换算成日期
    【工具】用hexo搭建博客
    【工具】文献分析工具histcite的简单使用
    【工具】用PPT排版打印海报时图片分辨率问题
    【工具】PPT插入高清图片保存后图片变模糊的解决方法
    【工具】排版软件TeX Live 2016的简单使用
    【工具】文字识别软件(OCR) ABBYY Finereader 11简单使用
    【Matlab】编程风格摘录
    【信号】用matlab实现一维信号的高斯滤波
    【GMT5】用GMT绘制测高卫星Topex_Poseidon_Jason的地面轨迹
  • 原文地址:https://www.cnblogs.com/peng14/p/3046359.html
Copyright © 2011-2022 走看看