一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

JS完成画圆圈的小球

时间:2017-06-03 编辑:简简单单 来源:一聚教程网

效果图

 

图(1)

图(2)

代码如下:

 

 代码如下 复制代码

JS动画之转动的小球

div{width:20px;height:20px;background-color:black;position:absolute;border:1px solid red;border-radius:50%;}

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();

 

热门栏目