<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./demo.css">
</head>
<body>
<div class="wrapper">
<div id="go">
<a href="#">Game</a>
</div>
<div id="main"></div>
</div>
<script src="./index.js"></script>
</body>
</html>
*{
padding: 0;
margin: 0;
text-decoration: none;
}
.wrapper{
position: relative;
width: 400px;
height: 600px;
margin: 150px auto;
overflow: hidden;
}
.wrapper #go{
width: 100%;
position: absolute;
top: 0;
text-align: center;
z-index: 95;
}
.wrapper #go a{
font-size: 60px;
color: cyan;
}
.wrapper #main{
width: 100%;
height: 100%;
position: relative;
top: -150px;
}
.row{
width: 100%;
height: 150px;
}
.row div{
float: left;
width: 99px;
height: 149px;
}
var go = document.getElementById('go');
var main = document.getElementById('main');
var colors = ['red', 'yellow', 'blue', 'cyan'];
var speed = 5,
num = 0,
timer;
flag = true;
function clickStart() {
go.addEventListener('click', function () {
go.style.display = 'none';
move();
})
}
clickStart();
function move() {
clearInterval(timer);
timer = setInterval(function () {
var step = parseInt(main.offsetTop) + speed;
main.style.top = step + 'px';
if (parseInt(main.offsetTop) >= 0) {
main.style.top = '-150px';
cDiv();
}
var len = main.childNodes.length;
if (len == 6) {
for (let i = 0; i < 4; i++) {
if (main.childNodes[len - 1].childNodes[i].classList.container('i')) {
alert('结束 ,得分:' + num);
clearInterval(timer);
flag = false;
}
}
main.removeChild(main.childNodes[len - 1])
}
}, 20);
binEvent();
}
function cDiv() {
var oDiv = document.createElement('div');
var index = Math.floor(Math.random() * 4);
oDiv.setAttribute('class', 'row');
for (let j = 0; j < 4; j++) {
var idiv = document.createElement('div');
oDiv.appendChild(idiv);
}
if (main.childNodes.length == 0) {
main.appendChild(oDiv);
} else {
main.insertBefore(oDiv, main.childNodes[0]);
}
var clickDiv = main.childNodes[0].childNodes[index];
clickDiv.setAttribute('class', 'i');
clickDiv.style.backgroundColor = colors[index];
}
function binEvent() {
main.addEventListener('click', function (e) {
if (flag) {
var tar = e.target;
if (tar.className == 'i') {
tar.style.backgroundColor = '#fff';
tar.classList.remove('i');
num++;
} else {
alert('结束 ,得分:' + num);
clearInterval(timer);
flag = false;
}
if (num % 10 == 0) {
speed++;
}
}
})
}