js----random随机小球

随机小球

<!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>随机小球</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        body {
            position: relative;

        }

        .dot {
            position: absolute;
            left: 0;
            top: 0;
            width: 100px;
            height: 100px;
            transition: all 1s ease-out;
            border-radius: 50%;
        }
    </style>
</head>

<body>
    <script>
        // 初始化加载
        window.onload = function () {
            createEle(createNumber(50));
            changeAnimation()
            let timer = null;
            const timeId = (func, interval) => {
                func();
                return setInterval(func, interval);
            };
            timer && clearInterval(timer);
            timer = timeId(changeAnimation, 1000);
        };
        // 创建数量
        function createNumber(nums) {
            let numberArr = [];
            let index = 0;
            do {
                index++;
                numberArr.push(index);
            } while (index < nums)
            return numberArr;
        }
        // 创建元素
        function createEle(smallNums) {
            smallNums.forEach(() => {
                let dot = document.createElement("div");
                dot.setAttribute("class", "dot");
                document.body.append(dot);
            })
        }
        // 获取元素
        function getEle() {
            let doms = document.getElementsByClassName("dot");
            return Array.from(doms);
        }
        // 获取随机值
        function getVariousVal(min, max) {
            min = Math.ceil(min);
            max = Math.floor(max);
            return Math.floor(Math.random() * (max - min + 1)) + min; // 含最大值 最小值
        }
        // 动画
        function changeAnimation() {
            let dotArr = getEle();
            dotArr.forEach((item) => {
                //生成随机左偏移量
                let dotLeft = getVariousVal(0, 1800);
                // 生成随机上偏移量
                let dotTop = getVariousVal(0, 800);
                // 生成随机rgb颜色----one
                let rgb_one = getVariousVal(0, 225);
                // 生成随机rgb颜色----two
                let rgb_two = getVariousVal(0, 225);
                // 生成随机rgb颜色----three
                let rgb_three = getVariousVal(0, 225);
                // 生成随机透明度
                let opacity = Math.random().toFixed(1);
                // 生成随机形状
                let circle = getVariousVal(0, 50);
                setVariousVal(item, dotLeft, dotTop, rgb_one, rgb_two, rgb_three, opacity, circle);
            })
        }
        // 设置随机值
        function setVariousVal(ele, left, top, colorOne, colorTwo, colorThree, opacity, circle) {
            // 设置左偏移量
            ele.style.left = left + "px";
            // 设置上偏移量
            ele.style.top = top + "px";
            // 设置透明度
            ele.style.opacity = opacity;
            // 设置随机形状
            ele.style.borderRadius = circle + "%";
            // 设置随机颜色
            ele.style.backgroundColor = `rgb(${colorOne},${colorTwo},${colorThree})`;
        }
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值