<!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="mystyle.css">
<title>Color Board2</title>
</head>
<body>
<div id="container">
</div>
<script src="myscript.js"></script>
</body>
</html>
*{
box-sizing: border-box;
}
body{
margin: 0;
background: #111;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
#container{
display: flex;
align-items: center;
justify-content: center;
max-width: 400px;
flex-wrap: wrap;
}
.square{
background: rgb(39, 37, 37);
width: 16px;
height: 16px;
margin: 2px;
box-shadow: 0 0 2px black;
transition: 2s ease;
}
.square:hover{
transition-duration: 0s;
}
const container = document.getElementById("container");
const colors = ['#363062','#ef962d','#fde2e2','#00bdaa','#00a8cc','#d8ebb5','#fbcffc','#f8615a','#a6b1e1','#f6eec7'];
const squareNum = 400;
function squareGenerator(){
for(let i=0;i<squareNum;i++){
const square = document.createElement('div');
square.classList.add('square');
square.addEventListener('mouseover',() => {
const color = getRandomColor();
square.style.background = color;
square.style.boxShadow = `0 0 2px ${color} 0 0 10px ${color}`;
})
square.addEventListener('mouseout',() => {
square.style.background = 'rgb(39, 37, 37)';
square.style.boxShadow = `0 0 2px black`;
})
container.append(square);
}
}
function getRandomColor(){
return colors[Math.floor(Math.random()*colors.length)];
}
squareGenerator()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/080c7d6d40bc7b2816d3b17bad7a97d4.gif)