当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS完成画圆圈的小球

JS完成画圆圈的小球

2019年06月13日  | 移动技术网IT编程  | 我要评论

效果图

 

图(1)

图(2)

代码如下:

<html>
<head>
<title>js动画之转动的小球</title>
<style type="text/css">
div{width:20px;height:20px;background-color:black;position:absolute;border:1px solid red;border-radius:50%;}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
var box=document.getelementbyid('box');
box.style.left="600";
box.style.top="300px";
var n=0;         //正弦函数的横坐标,理解为时间轴好一点。
function rotation()
{
 box.style.left=(600-math.sin(1/180)*80)+math.sin(n/180)*80+"px";  //300是小球的没开始运动的初始位置,n表示时间轴,后边是除数是为了将时间细分,使运动更平滑,80表示半径。
 box.style.top=(300-math.cos(1/180)*80)+math.cos(n/180)*80+"px";  //第一个括号中的内容是为了让小球在开始运动时处于初始位置(300,300)
 var dr = document.createelement('div');
 dr.style.left=(600-math.sin(1/180)*80)+math.sin(n/180)*80+"px";
 dr.style.top=(300-math.cos(1/180)*80)+math.cos(n/180)*80+"px";
 document.body.appendchild(dr);
 n++;
 if(n>180*2*math.pi)return false;  // 0 到 2π 为一个转动周期,如果要半圆,只需将n的取值范围减半,如需反方向转动,调换left和top的值即可。
 settimeout(rotation,1);
}
rotation();
</script>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网