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

代碼:
<!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";//小球初始坐標(biāo)x
ball.style.top=y+"px";//小球初始坐標(biāo)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ùn)動(dòng)速度
y=Math.floor(Math.sin(x/600*2*Math.PI)*300)+300;//小球運(yùn)動(dòng)的高度
if(x>600) x=0;
ball.style.left=x+"px";
ball.style.top=y+"px";
},100);
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- JavaScript+canvas實(shí)現(xiàn)框內(nèi)跳動(dòng)小球
- JavaScript結(jié)合Canvas繪畫畫運(yùn)動(dòng)小球
- JavaScript canvas實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)小球
- 原生js實(shí)現(xiàn)移動(dòng)小球(碰撞檢測(cè))
- js實(shí)現(xiàn)小球在頁面規(guī)定的區(qū)域運(yùn)動(dòng)
- 用js實(shí)現(xiàn)小球的自由移動(dòng)代碼
- js實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)的小球
- p5.js入門教程之小球動(dòng)畫示例代碼
- 原生js實(shí)現(xiàn)彈跳小球
- 原生js實(shí)現(xiàn)彈動(dòng)小球效果
相關(guān)文章
javascript 動(dòng)態(tài)table添加colspan\rowspan 參數(shù)的方法
動(dòng)態(tài)的給某個(gè)表對(duì)象添加列屬性和行屬性,采用obj.setAttribute("rowspan",n)(即rowspan=n)不能生效。2009-07-07
JavaScript常用基礎(chǔ)知識(shí)強(qiáng)化學(xué)習(xí)
這篇文章主要介紹了JavaScript常用基礎(chǔ)知識(shí)強(qiáng)化學(xué)習(xí),需要的朋友可以參考下2015-12-12

