[英]How can i make my Etch-A-Sketch grid change color and opacity when hovered?
I have been trying to do an etch a sketch assignment.我一直在尝试蚀刻草图作业。 I was able to produce the grid with the createGrids function , but with changeGridColor function and resetButton function added the grids created by createGrids function will disappear.我是能够产生与createGrids功能的网格,但changeGridColor功能和resetButton功能加入由createGrids函数创建的网格会消失。 And I have been trying to change the color of the grids to random colors and increase opacity when hovered but it's not working.我一直在尝试将网格的颜色更改为随机颜色并在悬停时增加不透明度,但它不起作用。 How will I achieve that, please?请问我将如何实现这一目标? Check the code.检查代码。
HTML HTML
<!DOCTYPE html>
<html>
<head>
<title>Etch-A-Sketch</title>
<link rel="stylesheet" href="etch-a-sketch.css">
</head>
<body>
<h1>Etch-A-Sketch</h1>
<div class="container"></div>
<div class="resetButton">Reset</div>
<script src="etch-a-sketch.js"></script>
</body>
</html>
CSS CSS
body{
text-align: center;
font-size: 16px;
width: 100%;
margin: 0px;
}
h1{
color: white;
font-size: 1.7rem;
font-weight: bolder;
height: 50px;
background-color: rgb(185, 113, 113);
margin-top: 0px;
line-height: 50px;
}
.container{
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 960px;
height: 960px;
border: 1px solid black;
margin: 0 auto;
}
.box{
border: 1px solid black;
}
.resetButton{
border: 1px solid black;
height: 40px;
width: 90px;
margin: 20px auto;
line-height: 40px;
border-radius: 10px
}
JS JS
let container = document.querySelector(".container");
let resetButton = document.querySelector("resetButton");
function createGrids(gridNumber = 16) {
let containerSize = Number(960);
let gridSize = Number(gridNumber);
for (let rowCol = 0; rowCol < gridSize ** 2; rowCol++){
let gridCell = document.createElement("div");
gridCell.style.height = `${(containerSize / gridSize) - 2}px`;
gridCell.style.width = `${(containerSize / gridSize) - 2}px`;
gridCell.classList.add("box");
container.appendChild(gridCell);
}
}
function changeGridColor(event) {
let a = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
let c = Math.floor(Math.random() * 256);
event.target.style.backgroundColor = `rgb(${a}, ${b}, ${c})`;
event.target.style.opacity += 0.1;
console.log(event)
}
function resetButton() {
let gridNumber = +prompt("Enter the grid size you want:", 16);
while (container.firstChild) {
container.removeChild(container.lastChild);
}
createGrids(gridNumber);
}
let resetButton = document.querySelector("resetButton");
resetButton.onclick = resetButton();
let gridCells = document.querySelectorAll(".box");
gridCells.forEach(cell => cell.addEventListener("mouseover", changeGridColor));
window.onload = function () {createGrids();};
mouseover
to the cells when creating them.创建时将mouseover
在单元格上。 let container = document.querySelector(".container"); let size = parseInt(window.getComputedStyle(container).width, 10); function createGrid(gridSize = 16) { for (let rowCol = 0; rowCol < gridSize ** 2; rowCol++) { let gridCell = document.createElement("div"); Object.assign(gridCell.style, { height : `${(size / gridSize) - 2}px`, width : `${(size / gridSize) - 2}px` }); gridCell.classList.add("box"); gridCell.addEventListener('mouseover', changeGridColor) container.appendChild(gridCell); } } let resetButton = document.querySelector(".resetButton"); resetButton.addEventListener('click', handleResetButton); window.onload = function() { createGrid(); }; function changeGridColor(e) { let color = `rgb(${[0, 0, 0].map(channel => { return Math.floor(Math.random() * 256); }).join(',')})` Object.assign(e.target.style, { backgroundColor : color, opacity : 0.25 }); } function handleResetButton(e) { let gridNumber = +prompt("Enter the grid size you want:", 16); while (container.firstChild) { container.removeChild(container.lastChild); } createGrid(gridNumber); }
body { text-align: center; font-size: 16px; width: 100%; margin: 0px; } h1 { color: white; font-size: 1.7rem; font-weight: bolder; height: 50px; background-color: rgb(185, 113, 113); margin-top: 0px; line-height: 50px; } .container { display: flex; flex-direction: row; flex-wrap: wrap; width: 400px; height: 400px; border: 1px solid black; margin: 0 auto; } .box { border: 1px solid black; } .resetButton { border: 1px solid #222; height: 40px; width: 90px; margin: 20px auto; line-height: 40px; border-radius: 10px; background: #F7F7F7; } .resetButton:hover { border: 1px solid black; background: #FFF; cursor: pointer; }
<h1>Etch-A-Sketch</h1> <div class="container"></div> <div class="resetButton">Reset</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.