样式:
<style>
body{
position: relative;父元素相对定位
}
div{
width: 200px;
height: 200px;
background-color: coral;
}
span{
position: absolute;子元素绝对定位
top: 10px;
left: 10px;
}
</style>
<script>
window.onload=function(){
var div=document.querySelector("div");
var span=document.querySelector("span");
div.onmousemove=function(e){
// 获取鼠标位置
span.style.top=e.y+"px";
span.style.left=e.x+"px";
console.log(e.x,e.y);
}
}
</script>
</head>
HTML内容:
<body>
<div></div>
<span>一段文字</span>
</body>
操作显示:
文字跟随鼠标移动

浙公网安备 33010602011771号