JavaScript实现随机数抽签小游戏:获取最小值决定谁取餐

作者:闭包旅人时间:2026-10-23 09:05:40浏览:585

本文介绍如何使用JavaScript实现一个“谁去拿外卖”的随机数小游戏。通过HTML构建弹窗结构,CSS完成样式布局,核心JS代码负责获取元素、处理鼠标交互事件,并利用自定义数组原型方法找出随机生成的数字序列中的最小值。当数组长度超过11时,通过移位机制保持最新数据,最终根据最小值索引确定结果,适合前端初学者学习DOM操作与算法逻辑。

随机数小游戏概述

本文展示了一个基于随机数生成的“谁去拿外卖”小游戏案例,供读者参考学习。

HTML与CSS结构实现

首先,需要构建基础的HTML页面结构,定义弹窗及其内部元素。


<div class="mask">
 <div class="contents">
  <div class="head">
   <p>谁去拿外卖</p>
   <a href="#" id="close">X</a>
  </div>
  <div class="cont-wapper">
   <div class="cont-inner">
    <h2></h2>
    <button></button>
    <div class="sign">随机到最小数字的人去拿外卖</div>
    <ul>
     <li class="takeout-list">扔出了一个2</li>
     <li>扔出了一个3</li>
    </ul>
   </div>
  </div>
 </div>
</div>

接着,编写CSS样式代码,设置弹窗的固定定位、背景透明度、边框及内部布局样式。


.mask { 
 position: fixed;left: 0;top: 0;
 width: 100%;height: 100%;
 background: rgba(0, 0, 0, 0.5);
}

.contents {
 position: absolute;top: 54px;left: 50%;
 width: 360px;border: 1px solid gray;background: white;
 border-radius: 5px;transform: translateX(-50%); 
}
.head {
 box-sizing: border-box;width: 100%;height: 44px;
 padding: 10px;border-bottom: 1px solid #eee; 
}
.head p {
 float: left;
}
.head a {
 float: right;width: 16px;
 line-height: 24px;color: #ccc;
}
.head a:hover {
 color: blue;
}
.cont-wapper {
 width: 300px;color: #555;
 padding: 15px 30px;margin: 0 auto;
}
 .cont-inner {
 font-size: 12px;background: #dbf0fa;
 padding-top: 15px;margin: 0 auto;
 margin-bottom: 10px;box-shadow: 1px 1px 2px #ddd;
}
 .cont-inner h2 {
 width: 186px;height: 188px;margin: 0 auto;
 background: url('../../Content/img1/ico.png') 0 -120px no-repeat;
}
.cont-inner button {
 display: block;cursor: pointer;/*箭头变手*/
 outline:none;/*去掉浏览器默认的外边框*/
 width: 271px;height: 40px;border: 0;
 background: url('../../Content/img1/ico.png') 0 0 no-repeat;
 margin: -45px auto 15px;
}
 .sign {
 position: relative;text-align: center;
 color: #777;margin-bottom: 10px;
}
/*after伪元素在元素之后添加内容*/
/*content 属性与 :before 及 :after 伪元素配合使用,来插入生成内容*/
.sign::after {   
 content: '';display: block;
 position: absolute;width: 40px;height: 7px;
 background: #ccc;right: 16px;top: 5px;
}
/*before伪元素在元素之前添加内容。*/
/*content 属性与 :before 及 :after 伪元素配合使用,来插入生成内容*/
 .sign::before {
 content: '';display: block;position: absolute;
 width: 40px;height: 7px;
 background: #ccc;left: 16px;top: 5px;
}
 .cont-inner ul {
 height: 180px;margin: 0 10px;
 padding: 5px 5px 0 5px;
 overflow: hidden;/*隐藏滚动条*/
}
.cont-wapper li.takeout-list {
 color: #fe5a23;font-weight: 600;
 height: 19px;line-height: 19px;
 background: url('../../Content/img1/ico.png') 0 -320px no-repeat;
}
.cont-wapper li {
 padding-left: 5px;
}

JavaScript核心逻辑与交互

在JavaScript部分,首先通过DOM方法获取页面中的关键元素,包括按钮、列表容器、弹窗层等,并初始化用于存储随机数的数组及最小值变量。


var button = document.getElementsByTagName('button')[0];//按钮
var ullist = document.getElementsByTagName('ul')[0];
var arrList = [];//创建数组
var mask = document.getElementsByClassName('mask')[0];
var text = document.getElementsByClassName('contents')[0];
var min = NaN;//最小值
var index;//索引值

随后,实现鼠标交互逻辑。当鼠标按下按钮时,背景图片位置动态变化并调用生成随机数的函数;鼠标松开时恢复背景状态。同时,处理鼠标移入移出事件,改变按钮背景以提供视觉反馈。


//鼠标按下事件
button.onmousedown = function () {
 this.style.backgroundPosition = '0 ' + (-80) + 'px';
 cteatNumer()//调用生成数组的方法
 //鼠标放开事件
 this.onmouseup = function () {
  this.style.backgroundPosition = '0 ' + (-40) + 'px';
 }
};
//鼠标移入事件
button.onmouseenter = function () {
 this.style.backgroundPosition = '0 ' + (-40) + 'px';
 //鼠标移出事件
 this.onmouseleave = function () {
  this.style.backgroundPosition = '0 ' + 0 + 'px';
 }
};

为了找出随机数中的最小值,扩展Array原型,添加min方法。该方法遍历数组,比较当前元素与已知最小值,返回数组中的最小数值。


//在数组中输出最小值
Array.prototype.min = function () {
 var min = this[0];//目前生成的数值
 var len = this.length;//数组目前的长度
 for (var i = 1; i < len; i++) {
  if (this[i] < min) {
   min = this[i];
  }
 }
 return min;
}

核心逻辑函数负责生成0到100之间的随机整数。若生成的数字与当前最小值重复,则重新生成;否则将数字加入数组。当数组长度超过11时,根据最小值索引情况执行删除或移位操作,保持数组大小,并更新最小值及其索引,最后刷新页面显示。


//数组取最小值
function cteatNumer() {
 var num = Math.floor(Math.random() * 100);//0-100之间随机生成一个精准的实数
 if (min == num) {//判断是否有最小值重复 
  cteatNumer();//有重复就重新生成
  return;
 }  
 arrList.push(num);//在数组最下面显示生成的值
 if (arrList.length > 11) {//数组长度超出11
  if (num > min && index == 0) {//当最小值索引值为0时
   arrList.splice(1, 1);//从数组索引值为1开始,删除第2个数值
  } else {
   arrList.shift();//数组往上移动
  }
 }
 min = arrList.min();//最小值
 index = arrList.indexOf(min);//最小值在数组中的索引
 refurbishDom(arrList, index);//调用refurbishDom方法
}

通过for循环遍历数组长度,配合上述逻辑,确保每次操作都能准确计算出当前序列中的最小值,从而确定“谁去拿外卖”的结果。


function refurbishDom(arr, index) {
 ullist.innerHTML = '';//清空ul所有的数值
 var len = arr.length;//获取当前数组的长度
 for (var i = 0; i < len; i++) {//显示对应索引的数值
  ullist.innerHTML += '<li>' + '扔出了一个' + arr[i] + '</li>';
 }
 //在ul数组中动态指定最小值
 ullist.getElementsByTagName('li')[index].className = 'takeout-list';
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

作者最新文章