JavaScript实现简单随机点名器
作者:菜_稽 时间:2024-04-10 16:18:39
本文实例为大家分享了js随机点名器的具体代码,供大家参考,具体内容如下
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>随机抽签</title>
</head>
<body>
<center>
<div id="d" style="background: #7A7A7A;width: 250px;"><!-- 设置背景框颜色和长度 -->
<h1 id = 'name'></h1> <!-- 用于显示随机的人名 -->
</div>
<div>
<button id = 'start' type="button">开始</button>
</div>
<div>
<button id = 'temp' type="button">暂停</button>
</div>
</center>
</body>
</html>
<script type="text/javascript">
window.onload=function(){
var names = ['name','name','name','name','name']; //人名数组
var clors = ['#EE0000','#CAE1FF','#008B8B','#CDC9C9','#F0F8FF'];//颜色数组
var name = document.getElementById('name'); //获取id为name的元素
var d = document.getElementById('d');
var temp = document.getElementById('temp');
var startR = -1; //记录计时器的序号
var start = document.getElementById('start');
function Random(){ //产生随机数,并更改h1中的内容与颜色
var nNum = Math.floor(Math.random()*5); //这里的5为你当前人名的数量
var cNum = Math.floor(Math.random()*5);
name.innerHTML = names[nNum];
d.style.color = clors[cNum];
}
start.onclick = function(){ //单击开始方法
if(startR == -1) //不加限制,多次点击开始会有多个计时器启动
startR = setInterval(Random,25);//设置计时器,每0.025秒执行一次
}
temp.onclick = function(){ //单击暂停方法
clearInterval(startR);
startR = -1;
}
}
</script>
当前代码仅实现了随机点名的功能,没有做任何界面美化。
具体展示
来源:https://blog.csdn.net/weixin_43687314/article/details/103119456
标签:js,点名器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python制作进度条的四种方法总结
2022-05-30 05:21:00
![](https://img.aspxhome.com/file/2023/1/78561_0s.gif)
Go语法糖之‘...’ 的使用实例详解
2024-04-26 17:16:43
mybatis-plus的sql语句打印问题小结
2024-01-25 11:23:42
python中redis的安装和使用
2021-02-21 23:19:44
vue3中关于路由hash与History的设置
2024-05-13 09:14:24
生成Jupyter Lab快捷方式的小技巧
2022-11-29 11:55:42
![](https://img.aspxhome.com/file/2023/5/135085_0s.png)
sql server 性能优化之nolock
2024-01-24 11:34:10
![](https://img.aspxhome.com/file/2023/6/128396_0s.png)
Python深度学习实战PyQt5布局管理项目示例详解
2023-03-11 08:47:37
![](https://img.aspxhome.com/file/2023/9/87519_0s.png)
Python NumPy教程之二元计算详解
2023-12-16 18:25:04
Python变量赋值的秘密分享
2021-10-12 11:53:04
![](https://img.aspxhome.com/file/2023/9/98889_0s.png)
通过SqlCmd执行超大SQL文件的方法
2024-01-21 00:35:50
![](https://img.aspxhome.com/file/2023/0/101540_0s.png)
Vue 实现拨打电话操作
2024-05-09 15:09:53
数据库查询优化(主从表的设计)
2024-01-18 16:25:52
mysql用户创建与授权的简单实例
2024-01-18 00:20:18
oracle 重置序列从指定数字开始的方法详解
2023-07-05 02:40:04
Python深度学习pytorch神经网络Dropout应用详解解
2023-01-02 11:18:54
![](https://img.aspxhome.com/file/2023/3/115613_0s.png)
python中文编码问题小结
2022-12-22 23:14:14
![](https://img.aspxhome.com/file/2023/2/133192_0s.png)
python如何将一个四位数反向输出
2023-03-21 16:42:40
Python进阶_关于命名空间与作用域(详解)
2022-03-17 17:29:02
Python Selenium XPath根据文本内容查找元素的方法
2022-02-18 12:55:16