今天herb同 学在twitter上问到,如何利用CSS使搜索条固定显示于窗口的某个位置。好像之前也碰过这个问题,不过,当时并没有解决,用JS有现成的方法,不 过,这次要求的就是不用JS。然后,开始写代码,测试,最终,IE6下依然有问题。position:fixed;没有正常显示。
在别的文章中看到,可以用position:absolute;来解决IE6的问题,不过,添加position:absolute;之后,依然没 有成功。当然,最终,还是用position:absolute;来解决。只是,不一定能成功。因为,有一句非常重要的话需要理解。
以下是代码片段:[www.xlnv.net]
fixed元素的绝对位置是相对于HTML元素来说,滚动条是body元素的。(via,刚才竟然没找到来源,囧。) |
只有记住了这句话,才知为什么position:absolute;很多地方都给出了结果,但当时并未能解决。因为html被我设置 position:relative。是上面这一句启发了我,最终才能够解决这个问题。我们拉动滚动条的时候,内容都会随着窗口滚动;这时滚动的是 body。如果让绝对定位的父级元素定为body,刚我们需要固定的某个模块将会固定在网页的某个位置,而不是固定在窗口的某个位置(貌似在 firefox中,html与body之间的介限并不明确?)。
我们需要做的是,让body保持其原有高度,让html只有一个窗口那么高。代码我们可以这样写:
以下是代码片段:[www.xlnv.net]
html{overflow:hidden;} // 重要! body{height:100%;overflow:auto;} |
这时,html将只有一个窗口那么高,超过的,直接hide。而body会随高度自动变化。这时,我们可以利用绝对定位来定位我们想要固定在窗口某个位置的模块。假设我们要固定的内容在右上角,代码可以这样写:
以下是代码片段:[www.xlnv.net]
html{overflow:hidden;} body{height:100%;overflow:auto;} #rightform form{position:absolute;right:30px;top50px;} |
这样,窗口就固定在右上角了。而其他浏览器,我们可以用position:fixed;来解决固定的问题。其他浏览器完整的代码如下:
以下是代码片段:[www.xlnv.net]
#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;} |
HTML代码
[www.xlnv.net]
<!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;<br /> Created by <a href="http://www.xlnv.net/" title="sofish">xlnv.net</a> from <a href="http://www.xlnv.net/" title="blog">小龙女</a></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>
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
转载自:http://www.cssdiy.com/Article/csshack/201104/1247.html