JS:鼠标跟随事件,代码不能执行,一直找不到错误在哪里?
代码如下:<style>div{width:20px;height:20px;background:red;position:absolute;}</style><scr...
代码如下:
<style>
div{
width:20px;height: 20px;background: red;position: absolute;
}
</style>
<script>
function getPos(ev){
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
var scrollLeft=document.documentElement.scrollLeft||document.body.scrollLeft;
return{x:ev.clientX+scrollLeft,
y:ev.clientY+scrollTop};
}
document.onmousemove=function(ev){
var aDiv=document.getElementsByTagName("div");
var oEvent=ev||event;
var pos=getPos(oEvent);
for(var i=aDiv.length-1;i>0;i--){
aDiv[i].style.left=aDiv[i-1].offsetLeft+"px";
aDiv[i].style.top=aDive[i-1].offsetTop+"px";
}
aDiv[0].style.left=pos.x+"px";
aDiv[0].style.top=pos.y+"px";
};
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
谁有心人帮忙找一下哪里有错误? 展开
<style>
div{
width:20px;height: 20px;background: red;position: absolute;
}
</style>
<script>
function getPos(ev){
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
var scrollLeft=document.documentElement.scrollLeft||document.body.scrollLeft;
return{x:ev.clientX+scrollLeft,
y:ev.clientY+scrollTop};
}
document.onmousemove=function(ev){
var aDiv=document.getElementsByTagName("div");
var oEvent=ev||event;
var pos=getPos(oEvent);
for(var i=aDiv.length-1;i>0;i--){
aDiv[i].style.left=aDiv[i-1].offsetLeft+"px";
aDiv[i].style.top=aDive[i-1].offsetTop+"px";
}
aDiv[0].style.left=pos.x+"px";
aDiv[0].style.top=pos.y+"px";
};
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
谁有心人帮忙找一下哪里有错误? 展开
2个回答
展开全部
<body>
<script type="text/javascript">
function mousePosition(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
function mouseMove(ev){
ev = ev || window.event;
var mousePos = mousePosition(ev);
document.getElementById('xxx').value = mousePos.x;
document.getElementById('yyy').value = mousePos.y;
document.getElementById('kk').style.left = mousePos.x+"px";
document.getElementById('kk').style.top = mousePos.y+"px";
}
document.onmousemove = mouseMove;
</script>
X:<input id="xxx" type="text" /> Y:<input id="yyy" type="text" />
<div id="kk" style="position:absolute;top:100px;">这里大部分代码来源于网络</div>
</body>
<script type="text/javascript">
function mousePosition(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
function mouseMove(ev){
ev = ev || window.event;
var mousePos = mousePosition(ev);
document.getElementById('xxx').value = mousePos.x;
document.getElementById('yyy').value = mousePos.y;
document.getElementById('kk').style.left = mousePos.x+"px";
document.getElementById('kk').style.top = mousePos.y+"px";
}
document.onmousemove = mouseMove;
</script>
X:<input id="xxx" type="text" /> Y:<input id="yyy" type="text" />
<div id="kk" style="position:absolute;top:100px;">这里大部分代码来源于网络</div>
</body>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询