1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. <style>
    9. #cont{
    10. width: 400px;
    11. height: 420px;
    12. position: relative;
    13. /* border: 1px solid red; */
    14. overflow: hidden;
    15. }
    16. #main{
    17. position: relative;
    18. top: -100px;
    19. }
    20. .row{
    21. width: 400px;
    22. height: 100px;
    23. }
    24. .row div{
    25. width: 98px;
    26. height: 98px;
    27. border: 1px solid gray;
    28. }
    29. .black{
    30. background: black;
    31. }
    32. </style>
    33. </head>
    34. <body>
    35. <div id="cont">
    36. <div id="main">
    37. </div>
    38. </div>
    39. <hr>
    40. <h3 id="f">0</h3>
    41. </body>
    42. <script>
    43. var main =document.getElementById('main');
    44. //div 工厂
    45. function cdiv(className){
    46. var div = document.createElement('div');
    47. if(className){
    48. div.setAttribute('class',className);
    49. }
    50. return div;
    51. }
    52. function crow(){
    53. var row = cdiv('row');//;创建行 div
    54. var index=Math.floor(Math.random()*4);
    55. //创建小div
    56. for(var i=0;i<4;i++){
    57. if (i==index) {
    58. //创建黑块
    59. row.appendChild(cdiv('black'));
    60. }else{
    61. //创建普通小块
    62. row.appendChild(cdiv());
    63. }
    64. }
    65. if (main.firstChild) {
    66. main.insertBefore(row,firstChild);
    67. }else{
    68. main.appendChild(row);
    69. }
    70. }
    71. //初始化函数
    72. function init(){
    73. //循环调用crow创建游戏界面
    74. for(var i= 0;i<4;i++){
    75. crow();
    76. }
    77. var sta = true;
    78. var sp =2;
    79. main.onclick=function (ev){
    80. var dj =ev.target;
    81. //如果点击白块,停止计时器
    82. if(sta ==false){
    83. clearInterval(clock);
    84. sta=false;
    85. alert('洗洗睡吧');
    86. }else if(dj.className==''){
    87. clearInterval(clock);
    88. sta= false;
    89. alert('洗洗睡吧');
    90. }else{
    91. dj.className='';
    92. dj.parentNode.pass=true;
    93. //得分
    94. var f = document.getElementById('f');
    95. var fs= parseInt(f.innerHTML)+1;
    96. f.innerHTML=fs;
    97. //根据分值实现加速
    98. if (fs%5==0) {
    99. sp+=2;
    100. }
    101. }
    102. }
    103. //设置定时器,反复调用move函数
    104. clock = setInterval('move()',50);
    105. }
    106. function move(){
    107. var t=parseInt(getComputedStyle(main).top);
    108. top +=sp;
    109. main.style.top= top+'px';
    110. if(top>=0){
    111. crow();
    112. main.style.top='-100px';
    113. //判断用户没有点击停止运行
    114. if(main.lastChild.pass== undefined){
    115. clearInterval(clock);
    116. sta=false;
    117. alert('洗洗睡吧');
    118. }
    119. if(main.children.length>5){
    120. main.removeChild(main.lastChild);
    121. }
    122. }
    123. }
    124. init();
    125. </script>
    126. </html>