1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } .root{ width:350px; height: 450px; border: solid 2px red; margin: 20px auto; font-size: 22px; font-weight: bold; cursor: pointer; } .time{ float: left; } .score{ float: right; } .big-text{ width: 100%; text-align: center; font-size: 200px; } .small-text{ list-style: none; width: 100%; height: 40px; display: flex; justify-content: space-around; padding-top: 50px; font-size: 32px; } </style> </head> <body> <div class="root"> <span class="time">剩余时间:15</span> <span class="score">分数:0</span> <p class="big-text">红</p> <ul class="small-text"> <li>红</li> <li>黄</li> <li>蓝</li> <li>绿</li> <li>紫</li> </ul> </div> </body> </html> <script type="text/javascript"> var scoreEl=document.getElementsByClassName("score")[0] var timeEl=document.getElementsByClassName("time")[0]; var bigEl=document.getElementsByClassName("big-text")[0]; var smallArray=document.getElementsByTagName("li"); var score=0; var time=15; var textArray=["红","黄","蓝","绿","紫"]; var colorArray=["red","yellow","blue","green","purple"]; var daan; function randomNum(min,max){ return Math.floor(Math.random()*(max-min+1)+min); } function changeBigText(){ var index=randomNum(0,textArray.length-1); bigEl.innerHTML=textArray[index]; var colorIndex=randomNum(0,colorArray.length-1); bigEl.style.color=colorArray[colorIndex]; daan=colorArray[colorIndex]; } changeBigText(); function changeSmallText(){ crashArray(textArray); crashArray(colorArray); for(var i=0;i<smallArray.length;i++){ smallArray[i].innerHTML=textArray[i]; smallArray[i].style.color=colorArray[i]; } } changeSmallText(); function crashArray(arr){ for(var i=0;i<10;i++){ var n1=randomNum(0,4); var n2=randomNum(0,4); if(n1!=n2){ var t=arr[n1]; arr[n1]=arr[n2]; arr[n2]=t; } } } for(var i=0;i<5;i++){ smallArray[i].onclick=function(){ console.log(getColorByText(this.innerHTML)); var color=getColorByText(this.innerHTML); if (color == daan) { score++; changeBigText(); changeSmallText(); scoreEl.innerHTML="分数"+score; } } } function getColorByText(text){ switch(text){ case "红": return "red"; case"黄": return "yellow"; case"蓝": return "blue"; case"绿": return "green"; case"紫": return "purple"; default: break; } } var s1=setInterval(function(){ time--; if (time==0) { clearInterval(s1); for(var i=0;i<smallArray.length;i++){ smallArray[i].onclick=null; } } timeEl.innerHTML="剩余时间:"+time; },1000); </script>
|