IE6 position:fixed bug (固定窗口方法) IE6,position

今天herb同学在twitter上问到,如何利用CSS使搜索条固定显示于窗口的某个位置。好像之前也碰过这个问题,不过,当时并没有解决,用JS有现成的方法,不过,这次要求的就是不用JS。

关键词:

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

正确的代码:预览/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”> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /> <title>IE6 position:fixed bug</title> <style> * { 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]–> </head> <body> <div id=”rightform”> <h1> <a href=”” title=”IE6 position:fixed bug” rel=”bookmark”>IE6 position:fixed bug</a> </h2> <p>position:fixed; vs position:absolute; support by <a href=”http://www.jb51.net/” title=”脚本之家”>脚本之家</a> from jb51.net</p> <form 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=”search” /> </form> </div> </body> </html>

提示:您可以先修改部分代码再运行

在别的文章中看到,可以用position:absolute;来解决IE6的问题,不过,添加position:absolute;之后,依然没有成功。当然,最终,还是用position:absolute;来解决。只是,不一定能成功。因为,有一句非常重要的话需要理解。

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;}

完整代码请看上面链接提供的txt文件。问题的原理就是这么简单。不知道,你能理解不?

IE6 position:fixed bug (固定窗口方法) IE6,position

相关文章:

你感兴趣的文章:

标签云: