原生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 相关文章推荐
juqery 学习之五 文档处理 插入
Feb 11 Javascript
jsPDF生成pdf后在网页展示实例
Jan 16 Javascript
Jquery对数组的操作技巧整理
Mar 25 Javascript
原生js获取宽高与jquery获取宽高的方法关系对比
Apr 04 Javascript
AspNet中使用JQuery boxy插件的确认框
May 20 Javascript
JQuery删除DOM节点的方法
Jun 11 Javascript
js中开关变量使用实例
Feb 24 Javascript
jQuery设置图片等比例缩小的方法
Apr 29 jQuery
vue loadmore组件上拉加载更多功能示例代码
Jul 19 Javascript
node.js学习之断言assert的使用示例
Sep 28 Javascript
单页面vue引入百度统计的使用方法示例详解
Oct 13 Javascript
详解vue中v-model和v-bind绑定数据的异同
Aug 10 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
提问的智慧(2)
2006/10/09 PHP
php报表之jpgraph柱状图实例代码
2011/08/22 PHP
php中cookie的使用方法
2014/03/29 PHP
WordPress中获取页面链接和标题的相关PHP函数用法解析
2015/12/17 PHP
php实现websocket实时消息推送
2018/03/30 PHP
DOM 脚本编程中的兄弟节点
2009/10/31 Javascript
js实现滑动触屏事件监听的方法
2015/05/05 Javascript
js实现的简单图片浮动效果完整实例
2016/05/10 Javascript
JavaScript仿flash遮罩动画效果
2016/06/15 Javascript
浅析ES6的八进制与二进制整数字面量
2016/08/30 Javascript
JavaScript实现自定义媒体播放器方法介绍
2017/01/03 Javascript
JavaScript正则表达式和级联效果
2017/09/14 Javascript
vue-cli初始化项目中使用less的方法
2018/08/09 Javascript
Vue CLI3.0中使用jQuery和Bootstrap的方法
2019/02/28 jQuery
jquery实现选项卡切换代码实例
2019/05/14 jQuery
javascript防抖函数debounce详解
2019/06/11 Javascript
js判断一个对象是数组(函数)的方法实例
2019/12/19 Javascript
JS数组方法join()用法实例分析
2020/01/18 Javascript
JSON 入门教程基础篇 json入门学习笔记
2020/09/22 Javascript
将Django使用的数据库从MySQL迁移到PostgreSQL的教程
2015/04/11 Python
python利用装饰器进行运算的实例分析
2015/08/04 Python
Python调用C# Com dll组件实战教程
2017/10/12 Python
详解Python中的分组函数groupby和itertools)
2018/07/11 Python
python 中文件输入输出及os模块对文件系统的操作方法
2018/08/27 Python
pycharm恢复默认设置或者是替换pycharm的解释器实例
2018/10/29 Python
python快速编写单行注释多行注释的方法
2019/07/31 Python
python实现修改固定模式的字符串内容操作示例
2019/12/30 Python
Python环境配置实现pip加速过程解析
2020/11/27 Python
迪卡侬中国官网:Decathlon中国
2020/08/10 全球购物
天游软件面试
2013/11/23 面试题
医大实习自我鉴定
2013/12/07 职场文书
鉴定评语大全
2014/05/05 职场文书
相亲大会策划方案
2014/06/05 职场文书
机械专业毕业生自我鉴定2014
2014/10/04 职场文书
小学二年级语文教学反思
2016/03/03 职场文书
OpenCV项目实践之停车场车位实时检测
2022/04/11 Python