拋出隨機數實現一個“誰取餐的小游戲”,供大家參考,具體內容如下
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"> <h2></h2> <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;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;}
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(1, 1);//從數組索引值為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';}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答