核心思想
"color: #ff0000">所用知识
"color: #ff0000">技术扩展
代码如下
"htmlcode">
<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>
"htmlcode">
<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>
"htmlcode">
<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实现随机点名项目的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 孙露《一抹伤HQ》头版限量[WAV+CUE][1G]
- 黄安.1989-一切从头(TP版)【天际唱片】【FLAC分轨】
- 群星.1994-浓情蜜意情歌精丫华纳】【WAV+CUE】
- 邓丽君.1983-淡淡幽情(2022环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 试音天碟《专业测试第一天碟》经典天碟精选[WAV分轨][1G]
- 试音典范 《情惹发烧情HQCD》人声发烧极品 [WAV+CUE][1G]
- 世界顶级汽车音响试音王《幸福在路上》[低速原抓WAV+CUE][1.1G]
- 老头杯第二届什么时候开始 英雄联盟第二届老头杯开赛时间介绍
- 老头杯第二届什么时候结束 英雄联盟第二届老头杯结束时间介绍
- 老头杯第二届规则是什么 英雄联盟老头杯第二届规则介绍
- 王崴-爵士听堂.蓝色波萨(HQCD)[WAV+CUE]
- 群星《欧美动听情歌·柔情第5季》2CD【DTS-WAV分轨】
- [极品珍藏]德意志进行曲集卡拉扬SACD[WAV+CUE]
- 前暴雪制作人呼吁反击DEI 玩家:夺回文化!
- 腾讯证实子公司Sharkmob大规模裁员:整个市场很低迷