核心思想

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

所用知识

  • 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. Unable to locate \.nuget\NuGet.exe 问题解决办法之一

    问题出现的原因是项目下.nuget文件夹下NuGet.exe文件夹不存在导致的 解决办法: 1.右键编辑NuGet.targets文件 将下载NuGet.exe的配置节点DownloadNuGetEx ...

  2. 走迷宫(用队列bfs并输出走的路径)

    #include <iostream> #include <stack> #include <string.h> #include <stdio.h> ...

  3. 集群(heartbeat)搭建

    HA 即(high available cluster)高可用集群,又称双机热备,保证关键性业务的不间断提供服务. 如:两台机器A和B,正常情况A提供服务,B待命闲置:一但A宕机或服务宕掉,自动切换至 ...

  4. 笔记本制作centos qcow2格式文件

    笔记本win7先通过vbox安装好centos6.5 然后打开cmd命令行在c:\Program Files\Oracle\VirtualBox下执行 vboxmanage clonehd --for ...

  5. canvas设置线条样式

    canvas设置线条样式 合法属性和方法 lineWidth = value 设置线宽 lineCap = type 设置线端点样式 lineJoin = type 设置线交合处样式 setLineD ...

  6. selenium+python自动化81-html报告优化(饼图+失败重跑+兼容python2&3)

    优化html报告 为了满足小伙伴的各种变态需求,为了装逼提升逼格,为了让报告更加高大上,测试报告做了以下优化: 测试报告中文显示,优化一些断言失败正文乱码问题 新增错误和失败截图,展示到html报告里 ...

  7. tomcat https 启用8443加证书

    <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF ...

  8. vs2010下使用绘图控件MsChart的方法

    1. 使用setupmschart.exe将MSCHRT20.OCX注册到系统: http://download.csdn.net/detail/xiaowh001/8892147 2. 在vs201 ...

  9. EmEditor的正则表达式

    前提是 "使用正则表达式"的复选框打上勾. 1 查找<>之间的字符串:   ".*?"2 查找双引号之间的字符串:   ".*?" ...

  10. Android给ListView添加一个入场动画

    动画是一个App体现良好交互的一种手段,通常的我们会看到很多App的ListView的Item都有一个入场动画例如: 可以看到,当进入界面加载ListView的Item的时候有一个向左滑动显示,并且淡 ...