JS 定位父级和偏移量
样例代码
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
height: 200px;
width: 200px;
padding: 10px;
margin: 10px;
overflow: auto;
border: 2px solid rebeccapurple;
background-color: aqua;
}
.smallBox {
position: absolute;
width: 50px;
height: 50px;
border: 2px solid rebeccapurple;
background-color: antiquewhite;
}
</style>
<body>
<div class="box">
<div class="smallBox"></div>
</div>
<script>
let oBox = document.querySelector(".box")
let oSmallBox = document.querySelector(".smallBox")
console.log(oSmallBox.offsetParent);
console.log(oSmallBox.offsetLeft);
console.log(oSmallBox.offsetTop);
</script>
</body>
JS 获取定位父级
offsetParent
console.log(oSmallBox.offsetParent);
JS 获取偏移量
子元素边框到父元素上边框和左边框距离
console.log(oSmallBox.offsetLeft);
console.log(oSmallBox.offsetTop);