<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body >
<div id="div1" style="width: 100px; height: 100px; position: absolute;"></div>
<script>
window.οnlοad=function(){
var oDiv=document.getElementById('div1');
document.οnmοusemοve=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>
转载于:https://www.cnblogs.com/chabai/p/5145429.html