原生JS实现随机点名项目

更新日期: 2019-04-30阅读: 2k标签: 项目

核心思想

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


所用知识

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);


原文地址:https://www.cnblogs.com/duxiu-fang/p/10795526.html 

 

链接: https://www.fly63.com/article/detial/3150

程序员最喜欢什么样的项目经理?

在当今的专业环境中,项目经理需要戴上各种帽子,在管理团队的日常功能和理解大局策略之间切换。正因为如此,项目经理对组织变得更有价值,并且他们对技能和战略角色的需求在全球范围内不断增长。但这也提出了一个问题:如何在如此高压的环境中成为更好的项目经理?

没有项目经验找工作处处碰壁怎么办?

我马上就要毕业了没有开发经验怎么办?我投递了 N 多公司全部没有给工作机会,有的给了面试机会也是没有下文了怎么办?我简历上什么东西都没有,要不要伪造一个工作经历呢?

当了项目经理才明白的10件事!

项目经理这个神奇的职位,改变了我很多工作处事的方式,从前性情纯真的耿直boy,现在变成了人鬼皆爱的老油条, 以下是我当了项目经理之后明白的10件事, 如有雷同,真是太巧。

pm2 快速部署前端项目

pm2 大家应该都知道,主要是用来管理 node 进程,但是同样可以用来部署前端代码。也可以手动添加 public key 到服务器上的 ~/.ssh/authorized_keys,

关于小型长周期项目的一些建议

我不是专业的项目经理,这里不讨论大型项目管理的事情。我们比较常遇到的可能是小型的长周期项目,比如2-4个人,做半年甚至一年的项目。这种项目通常不会有专职的项目经理

水印项目的实现以及两种实现方案的选优

通过 attachShadow 这个方法生成一个shadow root 即shadow的根节点,然后在这个根节点下面通过循环语句添加水印,利用position为absolute进行排版,使其铺满容器

重构项目,你真的准备好了吗?

我相信每个接受过老项目的程序员可能都吐槽过“前人的代码都是屎”。一个已经有些年头的项目,几乎肯定可以看到——到处拷贝来拷贝去的代码,随处可见的拼写错误,头重脚轻的函数……

浅谈RPC

近几年随着微服务化项目的崛起,逐渐成为许多公司中大型分布式系统架构的主流方式,而今天所说的 RPC 在这其中扮演着至关重要的角色。随着这段日子公司项目微服务化的演进,发现在日常开发中都在隐式或显式的使用 RPC

在Vue项目中使用Eslint+Prettier+Stylelint

首先搭建vue项目,lint选择ESLint + Prettier,配置方式选择In dedicated config files。具体搭建过程这里就不赘述了,如果不熟悉的同学可以点击这里。配置 Stylelint,目前还没有stylelint选项,需要我们自己安装相关的 npm 包

vue项目better-scroll使用注意点

created : 中请求数据,ajax是异步的,这个时候可能mounted已经执行完了,也就是dom挂载完了,但数据还没请求回来,无法获取到内部元素(数据渲染出来的dom)高度. 无法渲染内部元素,无法滚动

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!