為了把它放入上下文中,我正在嘗試制作一個蝕刻草圖游戲,其中的框?qū)⒏鶕?jù)用戶輸入改變大小,以始終填充網(wǎng)格容器。這就是為什么我不能設(shè)定一個標(biāo)準(zhǔn)的高度或?qū)挾取N以噲D根據(jù)用戶輸入創(chuàng)建一定數(shù)量的盒子。除了創(chuàng)建占據(jù)整個高度的矩形而不是盒子之外,它是有效的,并且它們根據(jù)輸入改變寬度。我正在嘗試制作可以環(huán)繞的盒子,而不是一個占據(jù)整個高度的長方形。我已經(jīng)亂七八糟的彈性包裝,不能解決它。
<body>
<div class="header">
<h1>Etch A Sketch</h1>
<div class="buttons">
<button class="clear">Clear</button>
<form id="inputForm">
<input type="number" class="number" min=1 max=64 placeholder="Enter a # 1-64">
</form>
</div>
</div>
<div id="gridContainer">
<div class="grid"></div>
</div>
body {
background-color: red;
min-height: 100vh;
height: 100vh;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
margin: 0;
}
h1 {
margin: 0;
}
#gridContainer {
width: 50%;
height: 80%;
background-color: white;
display: flex;
}
.grid {
flex-basis: 100%;
}
.header {
display: flex;
flex-direction: column;
margin-bottom: 5px;
width: 800px;
text-align: center;
font-size: 30px;
}
.buttons {
display: flex;
justify-content: space-between;
} ```
var form = document.getElementById("inputForm");
form.addEventListener("submit", function(e) {
e.preventDefault();
var input = form.querySelector("input");
var boxInput = input.value * input.value;
gridContainer = document.getElementById("gridContainer");
console.log(boxInput);
gridContainer.innerHTML = "";
for (var i = 0; i < boxInput; i++) {
var div = document.createElement("div");
console.log(div);
div.className = "grid";
gridContainer.appendChild(div);
}
});
既然您說flex不是必需的,那么下面就告訴您如何使用grid:
var form = document.getElementById("inputForm");
form.addEventListener("submit", function(e) {
e.preventDefault();
var input = form.querySelector("input");
var num = input.valueAsNumber;
var boxInput = num * num;
gridContainer = document.getElementById("gridContainer");
gridContainer.style.setProperty('--num', num);
//console.log(boxInput);
gridContainer.innerHTML = "";
for (var i = 0; i < boxInput; i++) {
var div = document.createElement("div");
//console.log(div);
div.className = "grid";
gridContainer.appendChild(div);
}
});
body {
background-color: red;
min-height: 100vh;
height: 100vh;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
margin: 0;
}
h1 {
margin: 0;
}
#gridContainer {
--num: 1;
width: 50%;
height: 80%;
background-color: white;
display: grid;
grid-template-columns: repeat(var(--num), 1fr);
grid-template-rows: repeat(var(--num), 1fr);
}
.grid {
/*For testing, remove this line if you don't need it*/
outline: 1px solid blue;
}
.header {
display: flex;
flex-direction: column;
margin-bottom: 5px;
width: 800px;
text-align: center;
font-size: 30px;
}
.buttons {
display: flex;
justify-content: space-between;
}
<div class="header">
<h1>Etch A Sketch</h1>
<div class="buttons">
<button class="clear">Clear</button>
<form id="inputForm">
<input type="number" class="number" min=1 max=64 placeholder="Enter a # 1-64">
<input type="submit">
</form>
</div>
</div>
<div id="gridContainer">
<div class="grid"></div>
</div>