h5实现自由落体

1、h5实现自由落体效果图
video.gif
2、h5实现自由落体源码
<!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; // 定时器

    function run(e) {
        clearInterval(_int);
        box = e;
        box.style.top = '1px'
        gravity = 1; // 重力加速度
        velocity = 1; // 初速度
        _int = setInterval(function () {
            // 更新速度(根据F=ma,这里忽略质量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))
            // 更新位置(根据s = ut + 0.5at^2,这里忽略空气阻力)
            box.style.top = (parseInt(box.style.top) + velocity) + 'px';
            // 重置速度,模拟摩擦力
            if (velocity > 0) {
                velocity -= 1; // 模拟摩擦力(这里假设摩擦力恒定)
            }
            if (parseInt(box.style.top) >= '600') {
                box.style.top = '1px'
            }
        }, 10); // 10毫秒更新一次
    }
</script>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容