随机点名器实现 相关代码 HTML CSS JS

本文介绍了一个使用HTML、CSS和JavaScript编写的点名器,它能随机显示预设的姓名列表,并在点击开始按钮后切换显示姓名和隐藏状态。
摘要由CSDN通过智能技术生成

实现效果图片:

代码:

<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点名器</title>
    <link rel="stylesheet" href="./css/common.css">
    <style>
        .box {
            width: 390px;
            height: 220px;
            background-image: url(./imgs/t015815bdb897cdb6ce.jpg);
            background-size: contain;
            border: 2px solid #000;
            border-radius: 20px;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            align-items: center;


        }

        .box1 {
            width: 150px;
            height: 50px;
            background-color: #fff;
            border-radius: 10px;
            line-height: 50px;
            text-align: center;


        }

        .box2 {
            width: 300px;
            height: 50px;

            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: space-around;
            color: #fff;


        }

        .left {
            width: 80px;
            height: 40px;
            background-color: rgba(0, 0, 0, 0.4);
            border-radius: 20px;
            line-height: 40px;
            text-align: center;

        }
    </style>
</head>

<body>
    <div class="box container">

        <div class="box1">
            姓名
        </div>
        <div class="box2 ">
            <div class="left">
                开始
            </div>

        </div>
    </div>
    <script>
        let content = document.querySelector('.box1');
        let begin = document.querySelector(".left");

        let name = ['刘畅', '刘晨旭', '谢帅', '王小慧', '候丁丁', '王展行', '王海洋']
        function randColor() {
            let r = Math.floor(Math.random() * 256);
            let g = Math.floor(Math.random() * 256);
            let b = Math.floor(Math.random() * 256);
            return `rgb(${r},${g},${b})`;
        }
        let timer = null;
        let falg = true;
        begin.onclick = function () {
            if (falg == true) {
                timer = setInterval(function () {
                    content.style.color = randColor();
                    // content.style.backgroundColor = randColor();
                    let index = Math.floor(Math.random() * name.length);
                    content.innerText = name[index];

                }, 50);
                begin.innerText = "结束"
                falg = false;
            }
            
             else {
                clearInterval(timer)
                begin.innerText = "开始"
                falg = true;
            }

        }

    </script>
</body>

</html>

commom.css

 *{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

li {
    list-style-type: none;
}

a {
    text-decoration: none;
}

.container {
    width: 960px;
    margin: 0 auto;

}

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的 web 随机点名实现,使用了 HTMLCSSJavaScript 和 PHP: 1. 创建一个名为 `index.html` 的 HTML 文件,用于显示随机点名的界面。可以使用 Bootstrap 和 jQuery 等库来快速构建界面,如下所示: ```html <!DOCTYPE html> <html> <head> <title>随机点名</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <style> body { background-color: #f2f2f2; padding-top: 50px; } .jumbotron { background-color: #e9ecef; text-align: center; } </style> </head> <body> <div class="container"> <div class="jumbotron"> <h1>随机点名</h1> <p>点击下面的按钮进行随机点名</p> <button class="btn btn-primary btn-lg" id="pickBtn">点名</button> </div> <div class="alert alert-success" id="result" style="display: none;"> <strong>被点名的同学是:</strong><span id="name"></span> </div> </div> <script> $(document).ready(function(){ $("#pickBtn").click(function(){ $.get("pick.php", function(data, status){ $("#name").text(data); $("#result").show(); }); }); }); </script> </body> </html> ``` 这个界面使用了 Bootstrap 的样式和组件,包括 jumbotron、btn、alert 等。添加了一个按钮用于触发点名程序,一个用于显示结果的 DIV,以及一个用于显示被选中的名字的 SPAN。使用了 jQuery 的 AJAX 方法来异步加载 PHP 文件中返回的结果。 2. 创建一个名为 `pick.php` 的 PHP 文件,用于处理随机点名的逻辑,如下所示: ```php <?php $names = array('张三', '李四', '王五', '赵六', '陈七'); $index = array_rand($names); echo $names[$index]; ?> ``` 这个 PHP 文件创建了一个包含多个名字的数组,使用 `array_rand()` 函数生成一个随机数作为选中的名字的索引。最后输出被选中的名字。 3. 将 `index.html` 和 `pick.php` 两个文件放在一个 PHP 服务上,比如 Apache 或 Nginx。打开 `index.html` 文件,点击按钮即可进行随机点名

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值