浮动和固定混合定位
这是 float 和 fixed 的标准用法:
<html>
<head>
<style type="text/css">
#bigDiv
{
background-color: red;
height: 2000px;
width: 100px;
float: left;
}
#littleDiv
{
background-color: green;
height: 400px;
width: 200px;
float: left;
}
#littleDivFixed
{
background-color: blue;
height: 100px;
width: 200px;
position: fixed;
}
</style>
</head>
<body>
<div id="bigDiv">
</div>
<div id="littleDiv">
</div>
<div id="littleDivFixed">
</div>
</body>
</html>
_
- “littleDiv”div 位于“bigDiv”div 的右侧,但不跟随滚动,
- “littleDivFixed”相反,“ div 会滚动,但相对于“bigDiv” div 的位置并不好(它总是卡在显示器的左侧)。
_
是否可以有一个混合这两种行为的 div :
- 始终位于“bigDiv”div 的右侧(距离恒定为 10px),
- 始终在显示屏上(与顶部保持 10 像素的恒定距离)?
_
预先感谢您的帮助。
here is a standard use of float and fixed :
<html>
<head>
<style type="text/css">
#bigDiv
{
background-color: red;
height: 2000px;
width: 100px;
float: left;
}
#littleDiv
{
background-color: green;
height: 400px;
width: 200px;
float: left;
}
#littleDivFixed
{
background-color: blue;
height: 100px;
width: 200px;
position: fixed;
}
</style>
</head>
<body>
<div id="bigDiv">
</div>
<div id="littleDiv">
</div>
<div id="littleDivFixed">
</div>
</body>
</html>
_
- the "littleDiv" div is on the right of the "bigDiv" div but does not follow the scrolling,
- the "littleDivFixed" div, on the contrary, scrolls but is not well positioned relatively to the "bigDiv" div (it is always stuck at the left of the display).
_
Is it possible to have a div that mixes the two behaviours :
- always on the right of the "bigDiv" div (at a constant distance of 10px),
- always on the display (at a constant distance of 10px from the top) ?
_
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样?只需将
left
和top
属性添加到fixed
divhttp://jsfiddle.net/t5bK9/
好的,这适用于 Chrome 和 IE8(确保它是标准模式,而不是怪癖),但由于某种原因不适用于 jsFiddle。我不知道为什么,但它做了你想要的(我认为)。如果您想确保它始终为 10px,以防 div 调整大小,您可以向 bigDiv 添加 onResize 侦听器以重新调用 positFix 函数。
Like this? Just add a
left
andtop
attribute to thefixed
divhttp://jsfiddle.net/t5bK9/
Ok, this works in Chrome and IE8 (make sure it's standards mode, not quirks), but for some reason not in jsFiddle. I'm not sure why, but it does what you want (I think). If you want to be sure it is always 10px right in case the divs get resized, you could add an onResize listener to bigDiv to re-call the positFix function.
您可以更改标记的结构吗?
我通过进行两项更改得到了您所描述的行为(在 Firefox 3.6 中):
在littleDiv 中嵌套了littleDivFixed
不必
因此,您
为固定div 添加边距
设置
margin
而不是left
可让您保持“自动”左侧定位,同时仍进行相对调整。Can you change the structure of the markup?
I got the behavior you described (in Firefox 3.6) by making two changes:
Nest littleDivFixed inside of littleDiv
So instead of
you have
Add a margin to the fixed div
Setting
margin
instead ofleft
lets you keep the "auto" left positioning, while still making relative adjustments.您也可以仅添加:
并设置:
以获得正确的布局。
JSFiddle
You can also only add:
and set:
for correct layout.
JSFiddle