<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
top: 0px;
margin: 0px;
}
.father{
position: relative;
width: 200px;
height: 200px;
margin-top: 100px;
margin-left: 100px;
background-color: red;
}
.son{
width: 50px;
height: 50px;
background-color: pink;
margin-left: 45px;
}
.box{
width: 200px;
height: 200px;
margin-left: 100px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
<div class="box"></div>
<script>
var father=document.querySelector(".father");
var son=document.querySelector(".son");
var box=document.querySelector(".box");
console.log(father.offsetTop);
console.log(father.offsetLeft);
console.log(son.offsetLeft);
console.log(son.offsetWidth);
console.log(son.offsetHeight);
console.log(son.offsetParent);
box.addEventListener("mousemove",function(e){
var mousex=e.pageX;
var mousey=e.pageY;
var boxx=box.offsetLeft;
var boxy=box.offsetTop;
var x=mousex-boxx;
var y=mousey-boxy;
box.innerHTML='x坐标是' + x + ' y坐标是' + y;
})
</script>
</body>
</html>