JS完成画圆圈的小球

<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>

欢迎分享本文,转载请保留出处:前端ABC » JS完成画圆圈的小球

分享到:更多 ()

发表评论 0