固定窗口方法 IE6 position:fixed bug

然后,开始写代码,测试,最终,IE6下依然有问题 。position:fixed;没有正常显示 。

固定窗口方法 IE6 position:fixed bug



正确的代码:预览/Demo | ie6_position_fixed_bug.txt(源代码)


!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"html xmlns="http://www.w3.org/1999/xhtml"headmeta http-equiv="Content-Type" content="text/html; charset=utf-8" /titleIE6 position:fixed bug/titlestyle* { padding:0; margin:0;}#rightform { text-align:center; padding:50px; font:14px/22px Georgia, "Times New Roman", Times, serif; height:1200px; background:#ffc;}#rightform h1 { font-family:arial; background:#e8edef; height:300px; line-height:300px; margin-bottom:200px;}#rightform p { line-height:1.5em; background:#ffdfff; padding:90px 0;}#rightform form { background-color:#ddd; padding:10px 20px; border:1px solid #aaa; position:fixed; right:30px; top:120px;}/style!--[if IE 6]style type="text/css" html{overflow:hidden;} body{height:100%;overflow:auto;} #rightform form{position:absolute;}/style![endif]--/headbodydiv id="rightform"h1a href="" title="IE6 position:fixed bug" rel="bookmark"IE6 position:fixed bug/a/h2pposition:fixed; vs position:absolute;support by a href="http://www.jb51.net/" title="我们"我们/a from jb51.net/pform id="g_search" method="get" action="#"input id="g_s" name="g_s" type="text" value=""/input id="g_submit" name="g_submit" type="button" value="https://www.rkxy.com.cn/dnjc/search" //form/div/body/html
ffcod = delpost.runcode21 .value; ffcod = ffcod.replace(/
/g,’’); delpost.runcode21 .value = https://www.rkxy.com.cn/dnjc/ffcod; 提示:您可以先修改部分代码再运行
在别的文章中看到,可以用position:absolute;来解决IE6的问题,不过,添加position:absolute;之后,依然没有成功 。当然,最终,还是用position:absolute;来解决 。只是,不一定能成功 。因为,有一句非常重要的话需要理解 。
【固定窗口方法 IE6 position:fixed bug】fixed元素的绝对位置是相对于HTML元素来说,滚动条是body元素的 。(via,刚才竟然没找到来源,囧 。)
只有记住了这句话,才知为什么position:absolute;很多地方都给出了结果,但当时并未能解决 。因为html被我设置position:relative 。是上面这一句启发了我,最终才能够解决这个问题 。我们拉动滚动条的时候,内容都会随着窗口滚动;这时滚动的是body 。如果让绝对定位的父级元素定为body,刚我们需要固定的某个模块将会固定在网页的某个位置,而不是固定在窗口的某个位置(貌似在firefox中,html与body之间的介限并不明确?) 。

我们需要做的是,让body保持其原有高度,让html只有一个窗口那么高 。代码我们可以这样写:

复制代码代码如下:
html{overflow:hidden;} // 重要!
body{height:100%;overflow:auto;}

这时,html将只有一个窗口那么高,超过的,直接hide 。而body会随高度自动变化 。这时,我们可以利用绝对定位来定位我们想要固定在窗口某个位置的模块 。假设我们要固定的内容在右上角,代码可以这样写:

复制代码代码如下:
html{overflow:hidden;}
body{height:100%;overflow:auto;}
#rightform form{position:absolute;right:30px;top50px;}

这样,窗口就固定在右上角了 。而其他浏览器,我们可以用position:fixed;来解决固定的问题 。其他浏览器完整的代码如下:

复制代码代码如下:
#rightform {text-align:center;padding:50px;font:14px/22px Georgia, "Times New Roman", Times, serif;height:1200px;background:#ffc;}
#rightform h1 {font-family:arial;background:#e8edef;height:300px;line-height:300px;margin-bottom:200px;}
#rightform p {line-height:1.5em;background:#ffdfff;padding:90px 0;}
#rightform form {background-color:#ddd;padding:10px 20px;border:1px solid #aaa;position:fixed;right:30px;top:120px;}

推荐阅读