JSFiddle - React, Tailwind, and code Playground
by artgas_pro
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
<script src="app.js"></script>
<title>Aim Training | Проект 5</title>
</head>
<body>
<div class="screen">
<h1>Aim Training</h1>
<a href="#" class="start" id="start">Начать игру</a>
</div>
<div class="screen">
<h1>Выберите время</h1>
<ul class="time-list" id="time-list">
<li>
<button class="time-btn" data-time="10">
10 сек
</button>
</li>
<li>
<button class="time-btn" data-time="20">
20 сек
</button>
</li>
<li>
<button class="time-btn" data-time="30">
30 сек
</button>
</li>
<li>
<button class="time-btn" data-time="45">
45 сек
</button>
</li>
</ul>
</div>
<div class="screen">
<h3>Осталось <span id="time">00:00</span></h3>
<div class="board" id="board"></div>
</div>
</body>
</html>
CSS
@import url("https://fonts.googleapis.com/css?family=Khula&display=swap");
* {
box-sizing: border-box;
}
body {
color: #fff;
font-family: "Khula", sans-serif;
height: 100vh;
overflow: hidden;
margin: 0;
text-align: center;
}
a {
color: #fff;
text-decoration: none;
}
a:hover {
color: #16d9e3;
}
.start {
font-size: 1.5rem;
}
h1 {
line-height: 1.4;
font-size: 4rem;
}
.screen {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
transition: margin 0.5s ease-out;
background: linear-gradient(90deg, #29323c 0%, #485563 100%);
}
.screen.up {
margin-top: -100vh;
}
.time-list {
display: flex;
flex-wrap: wrap;
justify-content: center;
list-style: none;
padding: 0;
}
.time-list li {
margin: 10px;
}
.time-btn {
background-color: transparent;
border: 2px solid #c0c0c0;
color: #fff;
cursor: pointer;
font-family: inherit;
padding: 0.5rem 1rem;
font-size: 1.5rem;
}
.time-btn:hover {
border: 2px solid #16d9e3;
color: #16d9e3;
}
.hide {
opacity: 0;
}
.primary {
color: #16d9e3;
}
.board {
display: flex;
justify-content: center;
align-items: center;
position: relative;
width: 500px;
height: 500px;
background: linear-gradient(118.38deg, #29323c -4.6%, #485563 200.44%);
box-shadow: -8px -8px 20px #2a333d, 10px 7px 20px #475462;
border-radius: 30px;
overflow: hidden;
}
.circle {
background: linear-gradient(90deg, #16d9e3 0%, #30c7ec 47%, #46aef7 100%);
position: absolute;
border-radius: 50%;
cursor: pointer;
}
JavaScript
window.onload = function () {
const screens = document.querySelectorAll('.screen');
const startBtn = document.getElementById('start');
const timeList = document.getElementById('time-list');
const timeEl = document.getElementById('time');
const board = document.getElementById('board');
const colors = ['#EE7217', '#FF00FE', '#7AE3D6', '#4447AD', '#E61A23', '#059A82', 'linear-gradient(90deg, #16d9e3 0%, #30c7ec 47%, #46aef7 100%)'];
let time = 0;
let score = 0;
startBtn.addEventListener('click', (e) => {
e.preventDefault();
screens[0].classList.add('up');
})
timeList.addEventListener('click', (e) => {
if (e.target.classList.contains('time-btn')) {
time = +e.target.getAttribute('data-time');
screens[1].classList.add('up');
startGame();
}
})
board.addEventListener('click', e => {
if (e.target.classList.contains('circle')) {
score++
e.target.remove();
createRandomCircle();
}
})
function startGame() {
setInterval(decreasetime, 1000);
createRandomCircle();
setTime(time);
}
function decreasetime() {
if (time == 0) {
finishGame();
} else {
let current = --time;
if (current < 10) {
current = `0${current}`;
}
setTime(current);
}
}
function setTime(value) {
timeEl.innerHTML = `00:${value}`;
}
function createRandomCircle() {
const circle = document.createElement('div');
const size = getRandomNamber(10, 60);
const { width, height } = board.getBoundingClientRect();
let color = getRandomColor();
const x = getRandomNamber(0, width - size);
const y = getRandomNamber(0, height - size);
circle.style.top = `${y}px`;
circle.style.left = `${x}px`;
circle.style.background = color;
circle.classList.add('circle');
circle.style.width = `${size}px`;
...