js实现简单的点名器随机色

js简单实现点名器随机色

布局(排版)

<body>
	<button onclick="star()">开始</button>
	<button onclick="stop()">结束</button>
	<div id="box">
	
	</div>
</body>

css样式

<style>
	#box{
		width: 240px;
		height: 400px;
	}
	#a{
		width: 80px;
		height: 40px;
		line-height: 40px;
		text-align: center;
		float: left;
		background: cyan;
	}
</style>

js代码

<script>
    //声明一个数组存取用户名
    const arr=['貂蝉','西施','杨玉环','王昭君','李白','赵匡胤','朱元璋','小乔','刘彻'];
    const box=document.getElementById('box');
    //声明一个全局变量
    let set;
    // console.log(box)
    // 动态创建div,把数组的数据放到div中
    for (var i = 0; i< arr.length; i++) {
        var div=document.createElement('div');
        div.id='a';
        div.innerHTML=arr[i];
        // console.log(div.innerHTML);
        box.appendChild(div);
    // 点击开始按钮随机选一个名字
    }
    function star(){
    // 开始之前先清除一遍定时器,防止出bug停止不了
        clearInterval(set);
        //设置一个定时器
        set=setInterval(() => {
            for(var k=0;k<arr.length;k++){
                box.children[k].style.background='';
            }
            var random = parseInt(Math.random() * arr.length);
            box.children[random].style.background = color();
        }, 100)
    }
    // 点击停止选取名字(清除定时器)
    function stop(){
        clearInterval(set);
    }
    //封装一个随机色
    function color(){
		const r = Math.floor(Math.random() * 255);
		const g = Math.floor(Math.random() * 255);
		const b = Math.floor(Math.random() * 255);
		const rgb='rgb('+r+','+g+','+b+')';
		return rgb;
	}
</script>
  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个简单的 web 随机点名实现,使用了 HTML、CSS、JavaScript 和 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` 文件,点击按钮即可进行随机点名
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值