简体   繁体   English

输入高时页面会减慢浏览器的速度

[英]Page slows browser down when input is high

I am building an etch-a-sketch browser version for the odin project.我正在为 odin 项目构建一个 etch-a-sketch 浏览器版本。 There is a prompt message that asks input from the user and creates a grid based on the input.有一条提示消息询问用户输入并根据输入创建一个网格。

If the input is 15 that should give a 15x15 grid.如果输入是 15,那么应该给出一个 15x15 的网格。

Unfortunately the higher the input the more time it takes for the page to load.不幸的是,输入越高,页面加载所需的时间就越多。 Any ideas why?任何想法为什么?

 const container = document.querySelector('#container'); const btn = document.querySelector('#btn'); /* btn.addEventListener('click', () => { squares.forEach(square => { square.style.backgroundColor = 'white'; }); }); */ btn.addEventListener('click', () => addGrid()); function addGrid() { let content = document.createElement('div'); let input = prompt("Please enter the number of squares per side (2 - 100)"); while ((input == null || input > 100 || input < 2 || isNaN(input))) { input = prompt("Please enter the number of squares per side (2 - 100)"); } for (let i = 0; i <= input * input; i++) { container.style.cssText = 'grid-template-rows: repeat(' + input + ' , 1fr); grid-template-columns: repeat(' + input + ', 1fr)'; content = document.createElement('div'); content.classList.add('square'); container.appendChild(content); squares = container.querySelectorAll('.square') squares.forEach(square => { square.addEventListener('mouseenter', () => { square.style.backgroundColor = 'black'; }); }); squares.forEach(square => { square.addEventListener('mouseleave', () => { square.style.backgroundColor = 'black'; }); }); } return content; }
 <button id="btn">Click</button> <div id="container"></div>

You need to move the event handlers outside the loop.您需要将事件处理程序移到循环之外。 Also you need to use < instead of <= in the loop您还需要在循环中使用 < 而不是 <=

Even better, delegate from container更好的是,来自容器的委托

Here is a working version - you could use calc() in the css to size the individual squares这是一个工作版本 - 您可以在 css 中使用 calc() 来调整各个方块的大小

Using delegated event handling使用委托事件处理

 const container = document.getElementById('container'); const btn = document.getElementById('btn'); const reset = document.getElementById('reset'); const mclick = e => { const tgt = e.target; if (tgt.matches(".square")) tgt.classList.toggle("clicked"); } const mhover = e => { const tgt = e.target; if (!tgt.matches(".square")) return; // not a square tgt.classList.toggle("active",e.type==="mouseover") } const resetGrid = () => container.querySelectorAll(".square") .forEach(sq => { ["clicked","active"] .forEach(cls => sq.classList.remove(cls)) }); const addGrid = () => { let content = document.createElement('div'); let input = prompt("Please enter the number of squares per side (2 - 100)"); while ((input == null || input > 100 || input < 2 || isNaN(input))) { input = prompt("Please enter the number of squares per side (2 - 100)"); } for (let i = 0; i < input * input; i++) { container.style.cssText = 'grid-template-rows: repeat(' + input + ' , 1fr); grid-template-columns: repeat(' + input + ', 1fr)'; let content = document.createElement('div'); content.classList.add('square'); content.textContent = i container.appendChild(content); } }; btn.addEventListener('click',addGrid); container.addEventListener("mouseover",mhover); container.addEventListener("mouseout",mhover); container.addEventListener("click",mclick); reset.addEventListener("click",resetGrid);
 #container { display:grid; } div.square { height: 50px; width: 50px; border:1px solid black;} div.square.active { background-color: black;} div.square.clicked { background-color: red;}
 <button id="btn">Start</button><button id="reset">Reset</button> <div id="container"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM