使用js实现动态小球气泡背景

11 篇文章 0 订阅
10 篇文章 0 订阅

废话不多说直接上代码

<!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>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .box {
        width: 100vw;
        height: 100vh;
        position: relative;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
    <script>
      function $(select) {
        return document.querySelector(select);
      }
      //   获取小球渲染的区域
      let _box = $(".box");

      //   生成随机数
      function suiji(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
      }
      let _width = parseInt(
        window.getComputedStyle(_box, null).getPropertyValue("width")
      );
      let _height = parseInt(
        window.getComputedStyle(_box, null).getPropertyValue("height")
      );
      function creatBall() {
        //   生成小球节点
        let _ball = document.createElement("div");
        let _size = suiji(30, 100);
        _ball.style.width = _ball.style.height = _size + "px";
        _ball.style.borderRadius = 50 + "%";
        //   生成小球的随机颜色
        let _colorPool = [
          "#0000FF",
          "#0099FF",
          "#00FFFF",
          "#0099CC",
          "#FF0000",
          "#996600",
        ]; //颜色池(方案一)
        let r = parseInt(suiji(0, 255));
        let g = parseInt(suiji(0, 255));
        let b = parseInt(suiji(0, 255));
        let bgcolor = `rgba(${r},${g},${b},.7)`;
        _ball.style.backgroundColor = bgcolor;
        // 随机小球的位置
        let _scopeW = _width - _size;
        let _scopeH = _height - _size;
        let _left = parseInt(suiji(0, _scopeW));
        let _top = parseInt(suiji(0, _scopeH));
        _ball.style.position = "absolute";
        console.log(_left);
        // 小球移动方向
        let _dirArr = ["left", "top", "right", "bottom"];
        let _dir = _dirArr[suiji(0, 3)];
        setInterval(() => {
          switch (_dir) {
            case "left":
              _left--;
              _dir = _left <= 0 ? "right" : _dir;
              break;
            case "right":
              _left++;
              _dir = _left >= _scopeW ? "left" : _dir;
              break;
            case "top":
              _top--;
              _dir = _top <= 0 ? "bottom" : _dir;
              break;
            case "bottom":
              _top++;
              _dir = _top >= _scopeH ? "top" : _dir;
              break;
          }
          _ball.style.left = _left + "px";
          _ball.style.top = _top + "px";
        }, 1000 / 90);
        _box.appendChild(_ball);
      }
      for (let i = 1; i < 100; i++) {
        creatBall();
      }
      function clearBall() {
        _box.removeChild(_ball);
      }
    </script>
  </body>
</html>

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

object not found

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值