获取偏移量 offset系列 js 230215

时间:2023-02-16 07:18:38

获取与定位了的父级的偏移量


属性

offsetTop

offsetLeft


示例


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box {
width: 500px;
height: 500px;
/*position: relative;*/
margin-top: 50px;
background-color: gold;
left: 100px;
top: 30px;
}

.in {
width: 50px;
height: 50px;
background-color: red;
position: absolute;
left: 20px;
top: 30px;
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
<script>
var eleIn = document.querySelector(".in")
console.log(eleIn.offsetTop)
</script>
</body>
</html>