h5實(shí)現(xiàn)自由落體

1、h5實(shí)現(xiàn)自由落體效果圖
video.gif
2、h5實(shí)現(xiàn)自由落體源碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Free Fall Animation</title>
    <style>
        #fallingBox {
            width: 50px;
            height: 50px;
            background-color: red;
            position: absolute;
            top: 1px;
            left: 50px;
        }
    </style>
</head>
<body>
<h1 id="t1"></h1>

<div id="fallingBox" onclick="run(this)"></div>

<script>
    // var box = document.getElementById('fallingBox');
    var box = document.getElementById('fallingBox');
    var gravity = 1; // 重力加速度
    var velocity = 0; // 初速度
    var _int = null; // 定時(shí)器

    function run(e) {
        clearInterval(_int);
        box = e;
        box.style.top = '1px'
        gravity = 1; // 重力加速度
        velocity = 1; // 初速度
        _int = setInterval(function () {
            // 更新速度(根據(jù)F=ma,這里忽略質(zhì)量m)
            velocity += gravity;
            document.getElementById("t1").innerText = ("box.style.top>>" + box.style.top)
            + ("\ngravity>>" + gravity)
            + ("\nvelocity>>" + velocity)
            + ("\nparseInt(box.style.top)>>" + parseInt(box.style.top))
            + ("\n(parseInt(box.style.top) + velocity)>>" + (parseInt(box.style.top) + velocity))
            // 更新位置(根據(jù)s = ut + 0.5at^2,這里忽略空氣阻力)
            box.style.top = (parseInt(box.style.top) + velocity) + 'px';
            // 重置速度,模擬摩擦力
            if (velocity > 0) {
                velocity -= 1; // 模擬摩擦力(這里假設(shè)摩擦力恒定)
            }
            if (parseInt(box.style.top) >= '600') {
                box.style.top = '1px'
            }
        }, 10); // 10毫秒更新一次
    }
</script>
</body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容