效果图
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 300px;
height: 300px;
background: lightskyblue;
margin: 100px auto;
position: relative;
cursor: pointer;
}
#box div {
width: 30px;
height: 30px;
background: red;
position: absolute;
}
</style>
</head>
<body>
<div id="box">
</div>
<script>
let box = document.getElementById("box");
let mark;
box.onmouseenter=function(){
mark= document.createElement("div");
box.appendChild(mark);
}
box.onmousemove=function(e){
mark.style.left = e.clientX-box.offsetLeft+15+"px"
mark.style.top = e.clientY-box.offsetTop+15+"px"
}
box.onmouseleave=function(){
box.removeChild(mark);
}
</script>
</body>
</html>