源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JS完成画圆圈的小球

  • 时间:2022-02-11 20:24 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JS完成画圆圈的小球
[b]效果图[/b]  [img]http://files.jb51.net/file_images/article/201703/201737140041234.png?20172714510[/img] 图(1) [img]http://files.jb51.net/file_images/article/201703/201737140054795.png?20172714434[/img] 图(2) [b]代码如下:[/b]
<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>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程素材网!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部