欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS代码实现圆周运动

程序员文章站 2022-03-03 22:47:13
js代码实现圆周运动

js代码实现圆周运动

<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			body{
				margin: 0;
				padding: 0;
			}
			#circle{
				position: relative;
				width: 300px;
				height: 300px;
				background: #f2f2f2;
				border-radius: 50%;
			}
			#ball{
				position: absolute;
				width: 10px;
				height: 10px;
				margin-left: -5px;
				margin-top: -5px;
				background: red;
				border-radius: 50%;
			}
		</style>
	</head>
	<body>
		<p id="circle">
			<p id="ball"></p>
		</p>
		<script type="text/javascript">
			var ocircle = document.getelementbyid("circle");
			var oball = document.getelementbyid("ball");
			
			var r = ocircle.offsetwidth/2;
			var angle = 0;
			
			var timer = setinterval(function(){
				angle++;
				var hd = math.pi/180*angle;
				
				var x = r*math.cos(hd);
				var y = r*math.sin(hd);
				
				oball.style.left =r+ x + "px";
				oball.style.top =r- y + "px";
				
				
			},30)
			
			
		</script>
	</body>
</html>