【发布时间】:2011-05-13 06:39:38
【问题描述】:
如何让 DIV 始终浮动在屏幕的右上角,这样即使向下滚动页面,DIV 仍会显示在相同的固定位置?谢谢。
【问题讨论】:
-
看这个链接,我想你想要粘性浮动页脚的反面。stackoverflow.com/questions/146659/…
标签: javascript css
如何让 DIV 始终浮动在屏幕的右上角,这样即使向下滚动页面,DIV 仍会显示在相同的固定位置?谢谢。
【问题讨论】:
标签: javascript css
使用position: fixed,并将其锚定到页面的top 和right 两侧:
#fixed-div {
position: fixed;
top: 1em;
right: 1em;
}
但是,IE6 不支持position: fixed。如果您在 IE6 中需要此功能,this purely-CSS solution 似乎可以解决问题。您需要一个包装器 <div> 来包含一些样式以使其工作,如 stylesheet 中所示。
【讨论】:
使用position:fixed,如前所述,IE6 无法识别position:fixed,但通过一些 css 魔法可以让 IE6 正常运行:
html, body {
height: 100%;
overflow:auto;
}
body #fixedElement {
position:fixed !important;
position: absolute; /*ie6 */
bottom: 0;
}
!important 标志使您不必对 IE 使用条件注释。这将使#fixedElement 在除IE 之外的所有浏览器中使用position:fixed,在IE 中,position:absolute 将与bottom:0 一起生效。这将为 IE6 模拟 position:fixed
【讨论】:
* html body #fixedElement { position: absolute; }。
!important 标志,则不应该。也就是说,当然交换两者也可以,不需要!important。