js實(shí)現(xiàn)隨機(jī)數(shù)小游戲
拋出隨機(jī)數(shù)實(shí)現(xiàn)一個(gè)“誰(shuí)取餐的小游戲”,供大家參考,具體內(nèi)容如下
1、HTML結(jié)構(gòu)代碼如下
<div class="mask">
<div class="contents">
<div class="head">
<p>誰(shuí)去拿外賣(mài)</p>
<a href="#" id="close">X</a>
</div>
<div class="cont-wapper">
<div class="cont-inner">
<h2></h2>
<button></button>
<div class="sign">隨機(jī)到最小數(shù)字的人去拿外賣(mài)</div>
<ul>
<li class="takeout-list">扔出了一個(gè)2</li>
<li>扔出了一個(gè)3</li>
</ul>
</div>
</div>
</div>
</div>
2、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;/*去掉瀏覽器默認(rèn)的外邊框*/
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偽元素在元素之后添加內(nèi)容*/
/*content 屬性與 :before 及 :after 偽元素配合使用,來(lái)插入生成內(nèi)容*/
.sign::after {
content: '';display: block;
position: absolute;width: 40px;height: 7px;
background: #ccc;right: 16px;top: 5px;
}
/*before偽元素在元素之前添加內(nèi)容。*/
/*content 屬性與 :before 及 :after 偽元素配合使用,來(lái)插入生成內(nèi)容*/
.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;/*隱藏滾動(dòng)條*/
}
.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;
}
3、js代碼獲取元素
var button = document.getElementsByTagName('button')[0];//按鈕
var ullist = document.getElementsByTagName('ul')[0];
var arrList = [];//創(chuàng)建數(shù)組
var mask = document.getElementsByClassName('mask')[0];
var text = document.getElementsByClassName('contents')[0];
var min = NaN;//最小值
var index;//索引值
4、js代碼實(shí)現(xiàn)鼠標(biāo)滑過(guò)的時(shí)候背景的動(dòng)態(tài)變化
//鼠標(biāo)按下事件
button.onmousedown = function () {
this.style.backgroundPosition = '0 ' + (-80) + 'px';
cteatNumer()//調(diào)用生成數(shù)組的方法
//鼠標(biāo)放開(kāi)事件
this.onmouseup = function () {
this.style.backgroundPosition = '0 ' + (-40) + 'px';
}
};
//鼠標(biāo)移入事件
button.onmouseenter = function () {
this.style.backgroundPosition = '0 ' + (-40) + 'px';
//鼠標(biāo)移出事件
this.onmouseleave = function () {
this.style.backgroundPosition = '0 ' + 0 + 'px';
}
};
5、js代碼實(shí)現(xiàn)在數(shù)組輸出最小值
//在數(shù)組中輸出最小值
Array.prototype.min = function () {
var min = this[0];//目前生成的數(shù)值
var len = this.length;//數(shù)組目前的長(zhǎng)度
for (var i = 1; i < len; i++) {
if (this[i] < min) {
min = this[i];
}
}
return min;
}
6、js代碼實(shí)現(xiàn)取出數(shù)組的最小值
//數(shù)組取最小值
function cteatNumer() {
var num = Math.floor(Math.random() * 100);//0-100之間隨機(jī)生成一個(gè)精準(zhǔn)的實(shí)數(shù)
if (min == num) {//判斷是否有最小值重復(fù)
cteatNumer();//有重復(fù)就重新生成
return;
}
arrList.push(num);//在數(shù)組最下面顯示生成的值
if (arrList.length > 11) {//數(shù)組長(zhǎng)度超出11
if (num > min && index == 0) {//當(dāng)最小值索引值為0時(shí)
arrList.splice(1, 1);//從數(shù)組索引值為1開(kāi)始,刪除第2個(gè)數(shù)值
} else {
arrList.shift();//數(shù)組往上移動(dòng)
}
}
min = arrList.min();//最小值
index = arrList.indexOf(min);//最小值在數(shù)組中的索引
refurbishDom(arrList, index);//調(diào)用refurbishDom方法
}
7、用for循環(huán)遍歷當(dāng)前數(shù)組的長(zhǎng)度
function refurbishDom(arr, index) {
ullist.innerHTML = '';//清空ul所有的數(shù)值
var len = arr.length;//獲取當(dāng)前數(shù)組的長(zhǎng)度
for (var i = 0; i < len; i++) {//顯示對(duì)應(yīng)索引的數(shù)值
ullist.innerHTML += '<li>' + '扔出了一個(gè)' + arr[i] + '</li>';
}
//在ul數(shù)組中動(dòng)態(tài)指定最小值
ullist.getElementsByTagName('li')[index].className = 'takeout-list';
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS生成隨機(jī)數(shù)的多種方法匯總(不同范圍、類(lèi)型的隨機(jī)數(shù))
- JavaScript中隨機(jī)數(shù)方法?Math.random()
- Javascript實(shí)現(xiàn)仿QQ隨機(jī)數(shù)驗(yàn)證
- Js生成隨機(jī)數(shù)/隨機(jī)字符串的方法小結(jié)【5種方法】
- JavaScript隨機(jī)數(shù)的組合問(wèn)題案例分析
- js控制隨機(jī)數(shù)生成概率代碼實(shí)例
- JavaScript生成指定范圍隨機(jī)數(shù)和隨機(jī)序列的方法
- javaScript產(chǎn)生隨機(jī)數(shù)的用法小結(jié)
- JS隨機(jī)數(shù)產(chǎn)生代碼分享
- JavaScript中獲取隨機(jī)數(shù)的幾種方法小結(jié)
相關(guān)文章
Javascript中浮點(diǎn)數(shù)相乘的一個(gè)解決方法
這篇文章主要介紹了Javascript中浮點(diǎn)數(shù)相乘的一個(gè)解決方法,需要的朋友可以參考下2014-06-06
javascript實(shí)現(xiàn)網(wǎng)頁(yè)背景煙花效果的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)網(wǎng)頁(yè)背景煙花效果的方法,涉及javascript數(shù)學(xué)運(yùn)算及頁(yè)面元素動(dòng)態(tài)操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
jsMind通過(guò)鼠標(biāo)拖拽的方式調(diào)整節(jié)點(diǎn)位置
這篇文章主要介紹了jsMind通過(guò)鼠標(biāo)拖拽的方式調(diào)整節(jié)點(diǎn)位置的方法,十分的簡(jiǎn)單實(shí)用,推薦給有需要的小伙伴參考下。2015-04-04
javascript淡入淡出效果的實(shí)現(xiàn)思路
這個(gè)思路是最近寫(xiě)XScroll.js類(lèi)的時(shí)候想明白的。平常我們說(shuō)的淡入淡出效果,一般分成兩部分,一半是淡入,另一半就是淡出了。不過(guò)經(jīng)過(guò)分析,我覺(jué)得其實(shí)只需要一半就行了2012-03-03
詳解JavaScript中的類(lèi)型判斷與類(lèi)型轉(zhuǎn)換
這篇文章主要給大家講解一下JavaScript中的類(lèi)型判斷與類(lèi)型轉(zhuǎn)換的基本概念和使用方法,對(duì)我們的學(xué)習(xí)JavaScript的類(lèi)型判斷與轉(zhuǎn)換有一定的幫助,需要的朋友可以參考下2023-07-07
js實(shí)現(xiàn)簡(jiǎn)單的倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡(jiǎn)單的倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01

