国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

js實現隨機數小游戲

2024-05-06 16:52:54
字體:
來源:轉載
供稿:網友

拋出隨機數實現一個“誰取餐的小游戲”,供大家參考,具體內容如下

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武林網。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 渝北区| 平罗县| 萨迦县| 儋州市| 温泉县| 大英县| 绩溪县| 东丽区| 阆中市| 汤原县| 会同县| 册亨县| 永州市| 湾仔区| 东明县| 泸州市| 射洪县| 三亚市| 正镶白旗| 常州市| 姜堰市| 阜城县| 玉环县| 万荣县| 内乡县| 广河县| 杨浦区| 荣成市| 兴文县| 玉树县| 吐鲁番市| 抚州市| 闽清县| 交城县| 大连市| 铜陵市| 罗江县| 尖扎县| 满洲里市| 麟游县| 乌审旗|