<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
width: 100px;
height: 100px;
background: #CCC;
position: absolute;
}
</style>
<script type="text/javascript">
function getPos(ev){
var scrollTop=document.getElement.scrollTop||document.body.scrollTop;
var scrolleft=document.getElement.scrolleft||document.body.scrolleft;
return{x:ev.clientX+scrollLeft,y:ev.clientY+scrollTop}
}
document.onmousemove= function(ev){
var oEvent=ev||event;
var odiv=document.getElementById("div1");
var pos=getPos(oEvent);
odiv.style.left=pos.x+"px";
odiv.style.top=pos.y+"px";
}
</script>
</head>
<body style="height: 2000px;">
<div id="div1"></div>
</body>
</html>
相关分类