我正在尝试使用绝对定位将元素“inner”与另一个 div“outer”的右上角齐平。 “内部”元素不是“外部”的子元素。因此,我需要使用 js 查找“外部”的定位并使用它。
但是,结果并不完全正确:
编辑:我无法重置正文 div 的边距/填充/相对值。 body 中还有其他元素。
#wrapper{background:red;
width:100px;
height:100px;}
#inner{
position:absolute;
background:green;
width:50px;
height:100px;
margin:0;padding:0;
}
#body{position:relative;}
<div id = "body">
<div id = "wrapper"></div>
<div id = "inner"></div>
</div>
var rect = document.getElementById("wrapper").getBoundingClientRect();
$("#inner").css({ 'top': String($(window).scrollTop()+rect.top)+"px" });
width_inner = $("#inner").width();
$("#inner").css({ 'left': String($(window).scrollLeft()+rect.right-width_inner)+"px" });
最佳答案
关于javascript - 使用绝对将元素放在其他元素内。元素是 sibling ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18783430/