zoukankan      html  css  js  c++  java
  • JS 实现双色球

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            #lotteryBalls {
                display: flex;
            }
            #lotteryBalls > div {
                height: 50px;
                 50px;
                border-radius: 50%;
                line-height: 50px;
                text-align: center;
                font-size: 20px;
                font-weight: bold;
                color: white;
                margin: 10px;
            }
            .red {
                background-color: red;
            }
            .blue{
                background-color: blue;
            }
        </style>

    </head>
    <body>
        <div id="userBalls"></div>
        <div id="lotteryBalls"></div>
    <script >
    /* 1. 生成球的数字 */
    function makeRandom(n, min, max) {
        // 创建一个数组用来保存生成的 n 个随机数
        let nums = [];
        // 控制生成随机数的个数
        for (let i = 0; i < n; i++) {
            // 生成一个范围 min 到 max 的随机数
            let num = Math.round(Math.random() * (max - min) + min);
            // 去重:判断随机数 num 是否存在于 nums 数组里面
            if (nums.indexOf(num) === -1) {
                // 不存在,添加进数组
                nums.push(num);
            } else {
                // 存在,说明当前这一个随机数作废。重新循环一次。
                i--;
            }
        }
        // 把最终的随机数数组返回出去
        return nums;
    }
    // 生成 6 个 1 到 33 的随机数,结果为数组。
    let redBalls = makeRandom(6, 1, 33);
    // 生成 1 个 1 到 16 的随机数,结果为数组。
    let blueBalls = makeRandom(1, 1, 16);

    /* 2. 数字添加到页面上 */
    /*
    makeBalls(): 将数组中的数字生成球,渲染到页面上
        arr: 随机数数组(红球的数组、篮球的数组)
        className: 不同的球有不同 class 属性名
    */
    function makeBalls(arr, className) {
        // 依次取出数组中的每一个元素
        for (let i = 0; i < arr.length; i++) {
            // 将取出来的每一个元素放到 <div> 标签中并渲染到页面上
            lotteryBalls.innerHTML += `<div class="${className}">${arr[i]}</div>`;
        }
    }
    // 渲染红球
    makeBalls(redBalls, "red");
    // 渲染蓝球
    makeBalls(blueBalls, "blue");
    </script>
    </body>
    </html>
  • 相关阅读:
    Git 常用命令整理
    用myeclipse打jar包,使其包含依赖jar包的指向
    解决 spring mvc3.1下post json出现HTTP Status 400 The request sent by the client was syntactically incorrect
    springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
    WIN8下git报错解决,fatal: Not a git repository (or any of the parent directories): .git
    Modernizr 用法
    SPRING MVC 的请求参数获取的几种方法
    spring mvc 缓存
    Spring+SpringMVC+Log4J
    Log4j使用总结
  • 原文地址:https://www.cnblogs.com/youwei716/p/11221065.html
Copyright © 2011-2022 走看看