Board
by webgleb
HTML
<div class="container" id="board">
</div>
CSS
* {
box-sizing: border-box;
}
body {
background-color: #111;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.container {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
max-width: 400px;
}
.square {
width: 16px;
height: 16px;
background: #1d1d1d;
margin: 2px;
box-shadow: 0 0 2px #000;
transition: 2s ease;
}
.square:hover {
transition-duration: 0s;
}
JavaScript
const board = document.querySelector('#board')
const colors = ['#1abc9c', '#2ecc71', '#3498db', '#9b59b6', '#34495e', '#16a085', '#27ae60',
'#2980b9', '#8e44ad', '#2c3e50', '#f1c40f', '#e67e22', '#e74c3c', '#ecf0f1', '#95a5a6',
'#f39c12', '#d35400', '#c0392b', '#bdc3c7', '#7f8c8d'];
const SQUARES_NUMBER = 500
for (let i = 0; i < SQUARES_NUMBER; i++) {
const square = document.createElement('div')
square.classList.add('square')
square.addEventListener('mouseover', () => {
setColor(square)
})
square.addEventListener('mouseleave', () => {
removeColor(square)
})
board.append(square)
}
function setColor(element) {
const color = getRandomColor();
element.style.backgroundColor = color;
element.style.boxShadow = `0 0 2px ${color}, 0 0 10px ${color}`
}
function removeColor(element) {
element.style.backgroundColor = '#1d1d1d';
element.style.boxShadow = `0 0 2px #000`
}
function getRandomColor() {
const index = Math.floor(Math.random() * colors.length);
return colors[index]
}