小球重力加速度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
div {
width: 28px;
height: 28px;
border-radius: 50%;
background-color: red;
position: absolute;
left: 0;
top: 200px;
}
</style>
</head>
<body>
<button id="fly">Flying</button>
<div id="ball"></div>
<script>
var speedX = 6, speedY = -16;
var timer = null,
fly = document.querySelector("#fly"),
ball = document.querySelector("#ball");
fly.onclick = function() {
clearInterval(timer);
timer = setInterval(function() {
ball.style.left = ball.offsetLeft + speedX + "px";
ball.style.top = ball.offsetTop + speedY + "px";
var maxT = window.innerHeight - ball.offsetHeight;
var maxL = window.innerWidth - ball.offsetWidth;
if (ball.offsetTop > maxT) {
ball.style.top = maxT + "px";
speedY *= -0.9;
speedX--;
if (speedX < 0) {
speedX = 0;
clearInterval(timer);
}
}
speedY++;
},20);
}
</script>
</body>
</html>