javascript动画之圆形运动,环绕鼠标运动作小球

时间:2021-05-26

代码如下:
复制代码 代码如下:
<script type="text/javascript">
var ball;
var mouseX = 100;
var mouseY = 100;
var angle = 0;
var radius = 50;

function run(){
if(ball === undefined){
ball = document.createElement("span");
ball.style.position = "absolute";
ball.style.color = "#FF0000";
ball.style.zIndex = 999999999;
ball.innerHTML = "●";
document.body.appendChild(ball);
}

ball.style["left"] = mouseX+(Math.cos(angle)*radius) + "px";
ball.style["top"] = mouseY+(Math.sin(angle)*radius) + "px";
angle+=0.1;
}
function MousePos(e)
{
e = e || window.event;
var x,y;
if(!document.all){
x = e.pageX;
y = e.pageY;
}
else{
x = event.clientX + document.documentElement.scrollLeft;
y = event.clientY + document.documentElement.scrollTop;
}
return {x:x,y:y};
}
function setXY(e)
{
e = e || window.event;
var pos = MousePos(e);
mouseX = pos.x;
mouseY = pos.y;
}
window.onload = function(){
setInterval(run,20);

document.documentElement.onmousemove = function(e){
e = e || window.event;
setXY(e);
};
}
</script>

作者: cnblogs airy

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章