代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>hoverboard</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #000;
}
.container {
width: 400px;
display: flex;
justify-content: center;
flex-wrap: wrap;
margin: 50px auto;
}
.square {
width: 16px;
height: 16px;
margin: 2px;
background-color: #1d1d1d;
box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
transition: all 1s ease;
}
.square:hover {
transition: 0s;
}
</style>
</head>
<body>
<div class="container"></div>
<script>
const container = document.querySelector('.container');
let nums_of_square = 500; // 小方格数量
console.time('代码执行时间'); // 开始计时
for (let i = 0; i < nums_of_square; i++) {
const square = document.createElement('div');
square.classList.add('square');
square.addEventListener('mouseover', () => setColor(square));
square.addEventListener('mouseout', () => removeColor(square));
container.appendChild(square);
}
console.timeEnd('代码执行时间'); // 结束计时并输出结果
const colors = ['#e74c3c', '#8e44ad', '#3498db', '#e67e22', '#2ecc71'];
function setColor(element) {
const color = getRandomColor();
element.style.background = color;
element.style.boxShadow = `0 0 2px ${color}, 0 0 10px ${color}`;
}
function removeColor(element) {
element.style.background = '#1d1d1d';
element.style.boxShadow = '0 0 2px #000';
}
function getRandomColor() {
return colors[Math.floor(Math.random() * colors.length)];
}
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。