<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 100px;
background-color: greenyellow;
position: relative;
margin: 50px;
}
#box p{
width: 300px;
height: 200px;
background-color: aquamarine;
position: absolute;
left: 100px;
top: 100px;
display: none;
/* 鼠标来到这个P标签上也不会触发事件---穿透 */
pointer-events: none;
/* 设置层级 */
z-index: 100;
}
</style>
</head>
<body>
<div id="box">
你的头像
<p>
你的介绍
</p>
</div>
<div>
111111
</div>
<script>
box.onmouseover = function(){
// console.log(this.firstElementChild)
this.firstElementChild.style.display = "block"
}
box.onmouseout = function(){
// console.log(this.firstElementChild)
this.firstElementChild.style.display = "none"
}
box.onmousemove = function(event){
// console.log(event.offsetX,event.offsetY)
// this.firstElementChild.style.left = event.offsetX+50+"px"
// this.firstElementChild.style.top = event.offsetY+50+"px"
this.firstElementChild.style.left = event.offsetX+"px"
this.firstElementChild.style.top = event.offsetY+"px"
}
</script>
</body>
</html>
效果显示: