<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body >
<div id="div1" style=" 100px; height: 100px; position: absolute;"></div>
<script>
window.onload=function(){
var oDiv=document.getElementById('div1');
document.onmousemove=function(ev){
var oEvent=ev||event;
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop; //解决chrome下scrollTop兼容性问题
var scrollLeft=document.documentElement.scrollLeft||document.body.scrollLeft;
oDiv.style.top=oEvent.clientY+scrollTop+'px'; //clientX与scrollTop一般要同时使用
oDiv.style.left=oEvent.clientX+scrollLeft+'px';
}
}
</script>
</body>
</html>