原生JS实现随机点名项目的实例代码


Posted in Javascript onApril 30, 2019

核心思想

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

所用知识

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

技术扩展

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

效果

原生JS实现随机点名项目的实例代码

代码如下

•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实现随机点名项目的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

Javascript 相关文章推荐
JavaScript 实现简单的倒计时弹窗DEMO附图
Mar 05 Javascript
JavaScript 定时器 SetTimeout之定时刷新窗口和关闭窗口(代码超简单)
Feb 26 Javascript
jQuery qrcode生成二维码的方法
Apr 03 Javascript
JavaScript操作表单实例讲解(上)
Jun 20 Javascript
js中window.open的参数及注意注意事项
Jul 06 Javascript
H5手机端多文件上传预览插件
Apr 21 Javascript
Angular.js中定时器循环的3种方法总结
Apr 27 Javascript
快速理解 JavaScript 中的 LHS 和 RHS 查询的用法
Aug 24 Javascript
详解基于vue-cli配置移动端自适应
Jan 13 Javascript
详解Angular6 热加载配置方案
Aug 18 Javascript
Vue实例的对象参数options的几个常用选项详解
Nov 08 Javascript
解决nuxt 自定义全局方法,全局属性,全局变量的问题
Nov 05 Javascript
vue实现随机验证码功能的实例代码
Apr 30 #Javascript
详解vue 图片上传功能
Apr 30 #Javascript
vue移动端屏幕适配详解
Apr 30 #Javascript
vue-cli3 项目优化之通过 node 自动生成组件模板 generate View、Component
Apr 30 #Javascript
微信小程序时间戳转日期的详解
Apr 30 #Javascript
使用 Vue cli 3.0 构建自定义组件库的方法
Apr 30 #Javascript
vue自动路由-单页面项目(非build时构建)
Apr 30 #Javascript
You might like
浅析php工厂模式
2014/11/25 PHP
PHP动态输出JavaScript代码实例
2015/02/12 PHP
PHP的Json中文处理解决方案
2016/09/29 PHP
php实现生成code128条形码的方法详解
2017/07/19 PHP
PHP封装的非对称加密RSA算法示例
2018/05/28 PHP
javascript 动态设置已知select的option的value值的代码
2009/12/16 Javascript
JS清空多文本框、文本域示例代码
2014/02/24 Javascript
AngularJS 实现按需异步加载实例代码
2015/10/18 Javascript
jquery淡入淡出效果简单实例
2016/01/14 Javascript
原生js获取元素样式的简单方法
2016/08/06 Javascript
Vue自定义指令使用方法详解
2017/08/21 Javascript
React通过父组件传递类名给子组件的实现方法
2017/11/13 Javascript
禁止弹窗中蒙层底部页面跟随滚动的几种方法
2017/12/07 Javascript
JavaScript引用类型之基本包装类型实例分析【Boolean、Number和String】
2018/08/09 Javascript
浅谈angularJS2中的界面跳转方法
2018/08/31 Javascript
jQuery表单选择器用法详解
2019/08/22 jQuery
JQuery使用数组遍历跳出each循环
2020/09/01 jQuery
JS相册图片抖动放大展示效果的示例代码
2021/01/29 Javascript
python迭代器的使用方法实例
2013/11/21 Python
用Python给文本创立向量空间模型的教程
2015/04/23 Python
python动态参数用法实例分析
2015/05/25 Python
Python 找到列表中满足某些条件的元素方法
2018/06/26 Python
Linux系统(CentOS)下python2.7.10安装
2018/09/26 Python
python对csv文件追加写入列的方法
2019/08/01 Python
python实现TCP文件传输
2020/03/20 Python
Python3爬虫关于识别点触点选验证码的实例讲解
2020/07/30 Python
Django和Ueditor自定义存储上传文件的文件名
2021/02/25 Python
乐高积木玩具美国官网:LEGO Shop US
2016/09/16 全球购物
Shopee越南:东南亚与台湾电商平台
2019/02/03 全球购物
澳大利亚儿童鞋在线:The Trybe
2019/07/16 全球购物
德国消费电子产品购物网站:Guter Kauf
2020/09/15 全球购物
什么是"引用"?申明和使用"引用"要注意哪些问题?
2016/03/03 面试题
公司年会抽奖活动主持词
2014/03/31 职场文书
2014年小学体育工作总结
2014/12/11 职场文书
个人年终总结开头
2015/03/06 职场文书
PHP实现rar解压读取扩展包小结
2021/06/03 PHP