【发布时间】:2016-06-01 02:10:32
【问题描述】:
我想创建一个固定元素,它在向下和向上滚动时将位于同一位置,但在调整窗口大小时也将在 x 轴上相对于不同的 div。
#blackbox {
width: 500px;
height: 2000px;
background-color: black;
color: white;
margin: 0 auto;
}
#floater {
width: 150px;
background-color: blue;
color: white;
position: fixed;
top: 50px;
right: 120px;
/* want here 10px on right from black box */
}
<html>
<div id="blackbox">
This is blackbox
<br> This is blackbox
<br> This is blackbox
<br> This is blackbox
<br> This is blackbox
<br>
</div>
<div id="floater">
Always 10px from black box
</div>
</html>
编辑: 我找到了解决方案here。
#floater {
left: calc(50% + 510px); /* 50% + blackbox width + wanted 10px space *.
}
【问题讨论】:
-
你能告诉我们什么对你有用吗?