JavaScript String 小球重力弹回

JavaScript String 小球重力弹回

复制代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.box{
width: 50px;
height: 50px;
border-radius: 50%;
background:purple;
position: absolute;
top: 0;
left: 0;
}
.line{
width: 500px;
border: 1px solid #333;
position: absolute;
top: 450px;
left: 0;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="line"></div>
</body>
<script type="text/javascript">
var obox=document.querySelector(".box");

var speed=5;//计步器
var target=400;//下落高度
document.onclick=function(){
setInterval(function(){
speed+=10;
var now=obox.offsetTop+speed;
if(now>=target){
now=target;
speed*=-1;//当小球到达底部停止,使他方向相反运动
speed*=0.9;//使小球有一定的速度

}
obox.style.top=now+"px";
},30)
}

</script>
</html>

复制代码

猜你喜欢

转载自www.cnblogs.com/li923/p/11448551.html