<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script>
//鼠标移动事件,加在document上,就是全局上
document.onmousemove=function (ev) {
var oEvent=ev||event;
var oDiv=document.getElementById("div1");
//高版本浏览器,已经直接用document.documentElement.scrollTop,基本兼容,但是低版本不行,还是做一下适配
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
var scrollLeft=document.documentElement.scrollLeft||document.body.scrollLeft;
oDiv.style.left=oEvent.clientX+scrollLeft+"px";
oDiv.style.top=oEvent.clientY+scrollTop+"px";
}
</script>
<style>
#div1{
width:100px;
height:150px;
background: red;
position: absolute;
}
</style>
<body >
<div id="div1">
</div>
</body>
</html>