javascript - 如何使我的 Etch-A-Sketch 网格在悬停时改变颜色和不透明度?

标签 javascript

我一直在尝试做一个 eclipse 刻草图作业。我能够使用createGrids函数生成网格,但使用changeGridColor函数resetButton函数添加了由createGrids函数创建的网格将会消失。我一直在尝试将网格的颜色更改为随机颜色并增加悬停时的不透明度,但它不起作用。请问我将如何实现这一目标?检查代码。

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

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

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();};

最佳答案

  1. 您正在向尚未添加到 DOM 的单元格添加监听器。
    • 创建单元格时将鼠标悬停添加到单元格中。
  2. 您还可以从容器元素中获取网格大小。

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>

关于javascript - 如何使我的 Etch-A-Sketch 网格在悬停时改变颜色和不透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60656460/

相关文章:

javascript - 在不重新加载页面的情况下加载 div 中的内容

javascript - 在直方图上画一条线 - d3

javascript - 解析 JSON 时 Google Cloud Dataflow Javascript UDF 错误

javascript - 如何在浏览器中防止 "Stop running this Script"?

javascript - (CSRF token 丢失或不正确。)在 Django 中使用 AJAX

javascript - document.scripts 不等于 document.getElementsByTagName ("script")

javascript - 使用 D3.js 将每五个 svg 元素包装在一个 div 中

javascript - 使用 jQuery 单击按钮时,如何让我的 CSS 动画运行?

javascript - 比较对象数组中的属性值

javascript - 从 react js中的对象数组中删除一个元素