首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Web中的鼠标自动搬动

2013-01-05 
Web中的鼠标自动移动!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.or

Web中的鼠标自动移动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body{
cursor:url('http://www.net320.com/random/images/hidden.cur'),auto;
}
</style>
<script type="text/javascript">
function $I(id){
return typeof(id)=="string"?document.getElementById(id):id;
};
var isMoving=false;
window.onload=function(){
$I("arrow").style.position="absolute";
$I("arrow").style.zIndex=1000;
document.body.onmousemove=function(e){
e=e||event;
if(!isMoving){
$I("arrow").style.left=e.clientX+document.documentElement.scrollLeft+1+"px";
$I("arrow").style.top=e.clientY+document.documentElement.scrollTop+1+"px";
}
};
document.oncontextmenu=function(e){
e=e||event;
if(isMoving){
e.returnValue=false;
}
};
$I("btn1").onclick=function(){
alert("lalala~~~~");
};
window.setInterval(function(){
if(!isMoving){
move(parseInt($I("arrow").style.left),parseInt($I("arrow").style.top),
parseInt($I("btn1").style.left)+$I("btn1").offsetWidth/2,
parseInt($I("btn1").style.top)+$I("btn1").offsetHeight/2)
}
},4000);
}
function getAngle(startX,startY,endX,endY)
{
var angle;
var tanx;
if(endX-startX!=0){
tanx=Math.abs(endY-startY)/Math.abs(endX-startX);
}else{
return 90+(endY-startY>0?180:0);
}
angle=Math.atan(tanx)/2/Math.PI*360;
if(endX-startX<0 && endY-startY<=0){
angle=180-angle;
}
if(endX-startX<0 && endY-startY>0){
angle+=180;
}
if(endX-startX>=0 && endY-startY>0){
angle=360-angle;
}
return angle;
};
function move(startX,startY,endX,endY){
var currentDistance=0;
var currentX=startX;
var currentY=startY;
var angle=getAngle(startX,startY,endX,endY);
var speed=5;
var intervalId=window.setInterval(function(){
if(Math.abs(parseInt($I("arrow").style.left)-endX)>speed
|| Math.abs(parseInt($I("arrow").style.top)-endY)>speed){
var newX,newY;
var xAngle=angle*(2*Math.PI/360);
var xDirection=Math.abs(Math.cos(xAngle))/Math.cos(xAngle);
var yDirection=Math.abs(Math.sin(xAngle))/Math.sin(xAngle);
if(Math.abs(Math.tan(xAngle))<=1){
var deltaX=Math.abs(speed*Math.cos(xAngle))*xDirection;
newX=currentX+deltaX;
newY=-(newX-startX)*Math.tan(xAngle)+startY;
}
else{
var deltaY=Math.abs(speed*Math.sin(xAngle))*yDirection;
newY=currentY-deltaY;
newX=-(newY-startY)/Math.tan(xAngle)+startX;
}
currentX=newX;
currentY=newY;
$I("arrow").style.left=currentX+"px";
$I("arrow").style.top=currentY+"px";
isMoving=true;
}
else{
window.clearInterval(intervalId);
isMoving=false;
$I("btn1").click();
$I("btn1").focus();
}
},10);
}
</script>
</head>
<body>
<input style="position:absolute;left:100px;top:30px;" type="button" id="btn1" value="I am a Button"/>
<div style="height:900px;"></div>
<img id="arrow" src="http://www.net320.com/random/images/arrow.gif"/>
</body>
</html>

热点排行