通过js原生来实现小球的碰撞反弹(不断迭代)

通过改变小球的left和top值来改变小球的移动 小球与小球之间的碰撞,要判断小球在被撞小球的哪个方向,从而判断小球该向哪个方向移动,同样的改变小球的坐标值,来实现小球的反弹

1. 先实现一个小球在固定容器内随机起点随机速度移动

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>case01</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .wrap {
            width: 800px;
            height: 300px;
            position: relative;
            margin: 0 auto;
            border: 1px solid black;
        }

        .ball {
            width: 40px;
            height: 40px;
            background-color: pink;
            border-radius: 50%;
            position: absolute;
            top: 130px;
            left: 0;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="ball"></div>
    </div>
</body>
<script>
    // s = v * t 
    var dBall = document.querySelector(".ball");
    var timer = null;
    var vx = 0;
    var vy = 0;
    // 自执行函数
    (function () {
        // 随机起点 随即速度
        vx = Math.random() > 0.5 ? rand(3, 10) : -rand(3, 10);
        vy = Math.random() > 0.5 ? rand(3, 10) : -rand(3, 10);
        // 起点
        dBall.style.left = rand(0, 760) + 'px';
        dBall.style.top = rand(0, 260) + 'px';
    })()
    // 开启定时器
    timer = setInterval(function () {
        // 每一次时间间隔,让dBall进行一次单位移动
        // dBall的新位置 = dBall的当前位置 + 速度
        // offsetLeft 距离定位父级的左侧偏移量 (只读)
        // offsetTop 距离定位父级的顶部偏移量 (只读)
        var l = dBall.offsetLeft + vx;
        var t = dBall.offsetTop + vy;
        // 边界处理
        if (t > 260) {
            t = 260;
            vy = -vy;
        }
        else if (t < 0) {
            t = 0;
            vy = -vy;
        }

        if (l > 760) {
            l = 760
            vx = -vx;
        }
        else if (l < 0) {
            l = 0;
            vx = -vx;
        }
        dBall.style.left = l + 'px';
        dBall.style.top = t + 'px';
    }, 30)

    // 随机函数 
    function rand(min, max) {
        return Math.round(Math.random() * (max - min) + min);
    }
</script>

</html>

实现效果图如下
在这里插入图片描述

2.实现多个小球在一固定容器内随机起点随机速度移动

代码段如下

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>case04</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .wrap {
            width: 800px;
            height: 300px;
            position: relative;
            margin: 0 auto;
            border: 1px solid black;
        }

        .ball {
            width: 40px;
            height: 40px;
            background-c
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值