代码如下(示例):
#divStart {
position: absolute;
width: 10px;
height: 10px;
border-radius: 50%;
background: black;
}
<script type="text/javascript">
window.onload = function () {
startMove("divStart", 5);
function startMove(id, n1) {
var sDiv = document.getElementById(id);
var iSpeed = 0;
setInterval(function () {
iSpeed += n1;
var t = sDiv.offsetTop + iSpeed;
// 触底 顶部
if (t > document.documentElement.clientHeight - sDiv.offsetHeight) {
t = document.documentElement.clientHeight - sDiv.offsetHeight;
iSpeed *= -1;
iSpeed *= 0.95;
}
sDiv.style.top = t + "px";
}, 100);
}
}
</script>
<body>
<div id="divStart"></div>
</body>