JavaScript實現小球沿正弦曲線運動
更新時間:2020年09月07日 14:12:59 作者:qq_34373065
這篇文章主要為大家詳細介紹了JavaScript實現小球沿正弦曲線運動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現小球沿正弦曲線運動的具體代碼,供大家參考,具體內容如下
效果圖:

代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.dot{
margin-top: 300px;
position: absolute;
width: 1px;
height: 1px;
background: #000000;
}
#ball{
width: 10px;
height: 10px;
background: red;
border: 1px solid red;
position: absolute;
border-radius: 10px;
}
</style>
</head>
<body>
<div style="position: relative; width: 600px;
height: 600px;
border: 1px solid #000000" id="draw_bo" >
</div>
<div id="ball" ></div>
<script type="text/javascript">
var dom=document.getElementById("draw_bo");
var ball=document.getElementById("ball");
var x=0,y=300;
ball.style.left=x+"px";//小球初始坐標x
ball.style.top=y+"px";//小球初始坐標y
for(var angle=0;angle<600;angle+=0.01){//畫正弦線
var childDot=document.createElement('div');
childDot.className='dot';
childDot.style.top=300*(Math.sin(angle*Math.PI/300))+"px";
childDot.style.left=angle+"px";
dom.appendChild(childDot);
}
var time=setInterval(function () {
x=x+5;//小球運動速度
y=Math.floor(Math.sin(x/600*2*Math.PI)*300)+300;//小球運動的高度
if(x>600) x=0;
ball.style.left=x+"px";
ball.style.top=y+"px";
},100);
</script>
</body>
</html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
javascript 動態(tài)table添加colspan\rowspan 參數的方法
動態(tài)的給某個表對象添加列屬性和行屬性,采用obj.setAttribute("rowspan",n)(即rowspan=n)不能生效。2009-07-07

