温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

js怎么实现随机数小游戏

发布时间:2021-04-19 12:39:01 阅读:296 作者:小新 栏目:web开发
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

小编给大家分享一下js怎么实现随机数小游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

具体内容如下

1、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">
    <h3></h3>
    <button></button>
    <div class="sign">随机到最小数字的人去拿外卖</div>
    <ul>
     <li class="takeout-list">扔出了一个2</li>
     <li>扔出了一个3</li>
    </ul>
   </div>
  </div>
 </div>
</div>

2、css样式代码如下

.mask { 
 position: fixed;left0;top0;
 width100%;height100%;
 backgroundrgba(0000.5);
}

.contents {
 position: absolute;top54px;left50%;
 width360px;border1px solid gray;background: white;
 border-radius5px;transformtranslateX(-50%); 
}
.head {
 box-sizing: border-box;width100%;height44px;
 padding10px;border-bottom1px solid #eee; 
}
.head p {
 float: left;
}
.head a {
 float: right;width16px;
 line-height24px;color#ccc;
}
.head a:hover {
 color: blue;
}
.cont-wapper {
 width300px;color#555;
 padding15px 30px;margin0 auto;
}
 .cont-inner {
 font-size12px;background#dbf0fa;
 padding-top15px;margin0 auto;
 margin-bottom10px;box-shadow1px 1px 2px #ddd;
}
 .cont-inner h3 {
 width186px;height188px;margin0 auto;
 backgroundurl('../../Content/img1/ico.png'0 -120px no-repeat;
}
.cont-inner button {
 display: block;cursor: pointer;/*箭头变手*/
 outline:none;/*去掉浏览器默认的外边框*/
 width271px;height40px;border0;
 backgroundurl('../../Content/img1/ico.png'0 0 no-repeat;
 margin: -45px auto 15px;
}
 .sign {
 position: relative;text-align: center;
 color#777;margin-bottom10px;
}
/*after伪元素在元素之后添加内容*/
/*content 属性与 :before 及 :after 伪元素配合使用,来插入生成内容*/
.sign::after {   
 content'';display: block;
 position: absolute;width40px;height7px;
 background#ccc;right16px;top5px;
}
/*before伪元素在元素之前添加内容。*/
/*content 属性与 :before 及 :after 伪元素配合使用,来插入生成内容*/
 .sign::before {
 content'';display: block;position: absolute;
 width40px;height7px;
 background#ccc;left16px;top5px;
}
 .cont-inner ul {
 height180px;margin0 10px;
 padding5px 5px 0 5px;
 overflow: hidden;/*隐藏滚动条*/
}
.cont-wapper li.takeout-list {
 color#fe5a23;font-weight600;
 height19px;line-height19px;
 backgroundurl('../../Content/img1/ico.png'0 -320px no-repeat;
}
.cont-wapper li {
 padding-left5px;
}

3、js代码获取元素

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;//索引值

4、js代码实现鼠标滑过的时候背景的动态变化

//鼠标按下事件
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';
 }
};

5、js代码实现在数组输出最小值

//在数组中输出最小值
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;
}

6、js代码实现取出数组的最小值

//数组取最小值
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(11);//从数组索引值为1开始,删除第2个数值
  } else {
   arrList.shift();//数组往上移动
  }
 }
 min = arrList.min();//最小值
 index = arrList.indexOf(min);//最小值在数组中的索引
 refurbishDom(arrList, index);//调用refurbishDom方法
}

7、用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';
}

以上是“js怎么实现随机数小游戏”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI

开发者交流群×