核心思想

  • 随机产生规定范围内的整数,然后再产生相同范围内的整数,两者相同时,则暂停。

所用知识

  • Math.random() * num: 产生从0到num的随机数
  • Math.floor(): 向下取整
  • 简单的DOM操作等

技术扩展

  • 扩展人数
  • 添加停止键等

效果

代码如下

  • html:
    <div class="container">
<section class="demo">
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</section>
<section class="students"><ul></ul></section>
<section class="buttonList">
<ul>
<li><button type="button">随机选一个</button></li>
<li><button type="button">随机选两个</button></li>
<li><button type="button">随机选三个</button></li>
</ul>
</section>
</div>
  • css:
    <style type="text/css">
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
body {
width: 100%;
height: 100%;
background: url("images/bg.jpg") no-repeat;
background-size: cover;
}
button {
border: none;
background-color: transparent;
color: #fff;
font-size: 20px;
}
.container {
width: 1200px;
height: 700px;
margin: 10px auto;
}
.container .demo, .container .buttonList {
width: inherit;
height: 25%;
}
.container .students {
width: inherit;
height: 50%;
}
.container .students li {
margin-right: 50px;
margin-bottom: 30px;
text-align: center;
border-radius: 10px;
font-size: 20px;
font-weight: bold;
}
.container .students li:nth-child(5n) {
margin-right: 0;
}
.container .buttonList li button {
float: left;
width: 200px;
height: 60px;
background-color: #4caf5085;
margin-right: 150px;
text-align: center;
line-height: 60px;
border-radius: 10px;
margin-top: 50px;
font-weight: bold;
}
.container .buttonList li button:hover {
background-color: #4caf50;
}
.container .buttonList li:nth-child(1) {
margin-left: 150px;
}
.container .demo li {
width: 200px;
height: 150px;
background-color: #4caf5085;
float: left;
margin-right: 150px;
border-radius: 50%;
margin-top: 10px;
line-height: 150px;
font-weight: bold;
color: #fff;
font-size: 60px;
text-align: center;
}
.container .demo li:first-child {
margin-left: 150px;
}
</style>
  • javascript:
<script type="text/javascript">
var stuArray = ["小方", "小田", "小明", "小红", "小吕", "小于", "小美", "小绿", "李华", "小李", "小胡",
"小夏", "小徐", "小小", "小吴", "小陈", "小狗", "小许", "小熊", "小新"];
var stuList = document.querySelector(".students").querySelector("ul");
var buttonList = document.querySelectorAll("button");
var demoList = document.querySelector(".demo").querySelectorAll("li"); for (var i = 0; i < stuArray.length; i++) {
var li = document.createElement("li");
stuList.appendChild(li);
li.innerHTML = stuArray[i];
li.style.cssFloat = "left";
li.style.width = 200 + "px";
li.style.height = 60 + "px";
li.style.backgroundColor = "#4caf5085";
li.style.color = "#fff";
li.style.lineHeight = 60 + "px";
} var stuArrayList = stuList.querySelectorAll("li"); function chooseOne () {
for (var i = 0; i < stuArrayList.length; i++) {
stuArrayList[i].style.background = "#4caf5085";
}
for (var i = 0; i < demoList.length; i++) {
demoList[i].innerHTML = "";
}
var interId = setInterval(function () {
var x = Math.floor(Math.random() * stuArray.length);
stuArrayList[x].style.backgroundColor = "green";
demoList[1].innerHTML = stuArrayList[x].innerHTML;
var timeId = setTimeout(function () {
stuArrayList[x].style.backgroundColor = "#4caf5085";
}, 100);
var y = Math.floor(Math.random() * stuArray.length);
if (y == x) {
clearTimeout(timeId);
clearInterval(interId);
stuArrayList[y].style.backgroundColor = "green";
}
}, 100);
} function chooseTwo () {
for (var i = 0; i < stuArrayList.length; i++) {
stuArrayList[i].style.background = "#4caf5085";
}
for (var i = 0; i < demoList.length; i++) {
demoList[i].innerHTML = "";
}
var interId = setInterval(function () {
do {
var x1 = Math.floor(Math.random() * stuArray.length);
var x2 = Math.floor(Math.random() * stuArray.length);
} while (x1 == x2);
stuArrayList[x1].style.backgroundColor = "green";
stuArrayList[x2].style.backgroundColor = "green";
demoList[0].innerHTML = stuArrayList[x1].innerHTML;
demoList[2].innerHTML = stuArrayList[x2].innerHTML;
var timeId = setTimeout(function () {
stuArrayList[x1].style.backgroundColor = "#4caf5085";
stuArrayList[x2].style.backgroundColor = "#4caf5085";
}, 100);
var y1 = Math.floor(Math.random() * stuArray.length);
var y2 = Math.floor(Math.random() * stuArray.length);
if ((y1 == x1 && y2 == x2) || (y1 == x2 && y2 == x1)) {
clearTimeout(timeId);
clearInterval(interId);
stuArrayList[x1].style.backgroundColor = "green";
stuArrayList[x2].style.backgroundColor = "green";
}
}, 100);
} function chooseThree () {
for (var i = 0; i < stuArrayList.length; i++) {
stuArrayList[i].style.background = "#4caf5085";
}
for (var i = 0; i < demoList.length; i++) {
demoList[i].innerHTML = "";
}
var interId = setInterval(function () {
do {
var x1 = Math.floor(Math.random() * stuArray.length);
var x2 = Math.floor(Math.random() * stuArray.length);
var x3 = Math.floor(Math.random() * stuArray.length);
} while (x1 == x2 || x2 == x3 || x1 == x3);
stuArrayList[x1].style.backgroundColor = "green";
stuArrayList[x2].style.backgroundColor = "green";
stuArrayList[x3].style.backgroundColor = "green";
demoList[0].innerHTML = stuArrayList[x1].innerHTML;
demoList[1].innerHTML = stuArrayList[x2].innerHTML;
demoList[2].innerHTML = stuArrayList[x3].innerHTML;
var timeId = setTimeout(function () {
stuArrayList[x1].style.backgroundColor = "#4caf5085";
stuArrayList[x2].style.backgroundColor = "#4caf5085";
stuArrayList[x3].style.backgroundColor = "#4caf5085";
}, 100);
var y1 = Math.floor(Math.random() * stuArray.length);
var y2 = Math.floor(Math.random() * stuArray.length);
var y3 = Math.floor(Math.random() * stuArray.length);
if ((x1 == y1 && x2 == y2) || (x1 == y2 && x2 == y1)) {
clearTimeout(timeId);
clearInterval(interId);
stuArrayList[x1].style.backgroundColor = "green";
stuArrayList[x2].style.backgroundColor = "green";
stuArrayList[x3].style.backgroundColor = "green";
}
}, 100);
} buttonList[0].addEventListener("click", function () {chooseOne()}, false);
buttonList[1].addEventListener("click", function () {chooseTwo()}, false);
buttonList[2].addEventListener("click", function () {chooseThree()}, false);

原生JS实现随机点名项目的更多相关文章

  1. 使用原生js 获取用户访问项目的浏览器类型

    想要获取浏览器的类型很简单,网上提供了很多方法,但是看过之后,都是根据浏览器内核来判断是ie,谷歌,火狐,opeara的, 所以不能进一步判断在国内使用的主流浏览器类型,比如360,百度,搜狐浏览器等 ...

  2. 原生JS实现简易随机点名功能

    定时器的工作原理,这里将用引用How JavaScript Timers Work中的例子来解释定时器的工作原理,该图为一个简单版的原理图.· 上图中,左侧数字代表时间,单位毫秒:左侧文字代表某一个操 ...

  3. 原生js实现简单的随机点名系统

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  4. 记一次数据、逻辑、视图分离的原生JS项目实践

    一切的开始源于这篇文章:一句话理解Vue核心内容. 在文章中,作者给出了这样一个思考: 假设现在有一个这样的需求,有一张图片,在被点击时,可以记录下被点击的次数. 这看起来很简单吧, 按照上面提到到开 ...

  5. js - 面向对象 - 小案例:轮播图、随机点名、选项卡、鼠标拖拽

    面向对象 对象 : (黑盒子)不了解内部结构, 知道表面的各种操作. 面向对象 : 不了解原理的情况下 会使用功能 . 面向对象是一种通用思想,并非编程中能用,任何事情都能用. 编程语言的面向对象的特 ...

  6. js随机点名

    定时器案例. <!-- Author: XiaoWen Create a file: 2016-12-08 12:27:32 Last modified: 2016-12-08 12:51:59 ...

  7. js随机点名系统

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. JS: 随机点名程序与万年历

    随机点名程序 document.write(Math.random()); var stu = ["张三", "王五", "张二", &qu ...

  9. Ajax进阶之原生js与跨域jsonp

    什么是Ajax? 两个数求和: 用Jquery和数据用json格式 viws函数: from django.shortcuts import render,HttpResponse # Create ...

随机推荐

  1. mysql 计算经纬度函数(米)

    ) CHARSET utf8mb4 begin return ROUND( * ASIN( SQRT( POW( SIN( ( lat1 ) ), ) ) ) * POW( SIN( ( lon1 ) ...

  2. C++ 实例化对象 p->printX()

    一.从栈实例化对象 我们首先定义一个类,类的名字叫TV,里面包括两个成员变量,两个成员函数. class TV // 定义一个电视的类TV { public: ]; // 定义类的属性,一个数组 in ...

  3. [转]Jsp 与 JavaBean

    JavaBean 是一个遵循特定写法的 Java 类,它有以下特点: 1. Java 类具有一个无参的构造函数 2. 属性必须私有化. 3. 私有化的属性通过 public 类型的方法暴露给其它程序, ...

  4. jetty之嵌入式开发

    一.Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可 ...

  5. 使用Fiddler实现网络限速

    Fiddler实现网络限速方法: 1.点击FiddlerScript 2.在脚本里相应的地方添加“2”处两行代码(不加注释),保存(Save Script) 第一行为请求延时3秒,第二行为响应延时1. ...

  6. linux编程vim设置

    linux环境下c网络编程vim编辑工具设置,包括自动缩进,tab键对齐等.

  7. 224. Basic Calculator + 227. Basic Calculator II

    ▶ 两个四则表达式运算的题目,第 770 题 Basic Calculator IV 带符号计算不会做 Orz,第 772 题 Basic Calculator III 要收费 Orz. ▶ 自己的全 ...

  8. MVC表单提交写法1

    初学MVC,感觉跟以前的aspx页面差别很大,我们就先来看看MVC的表单是怎么提交的吧. 现在我们用一个最简单的例子来看一看MVC是怎么提交表单的(这一个例子中,我们的关注点是如何提交表单,所以不涉及 ...

  9. S SQL

    样品申请单状态更新为“审核不通过” select STATUS_CD from S_ORDER where row_id='1-5U7IL' update S_ORDER set STATUS_CD= ...

  10. This usually happens because your environment has changed since running `npm install`

    此时运行按照提示执行  npm rebuild node-sass  命令,(如若不行,则先运行npm install node-sass命令执行) 然后再运行 node命令,启动服务.