如何创建一个网格,其中可变数量的正方形会自动调整大小以适应空间
How create a grid in which a variable number of squares automatically resize to fit a space?
我正在解决这个问题:http://www.theodinproject.com/web-development-101/javascript-and-jquery?ref=lnav。
基本上,我需要创建一个游戏,其中用户单击一个按钮,询问他们想要在网格中有多少个方块,然后网格出现,他们可以通过将鼠标悬停在改变颜色的方块上在网格上绘制。到目前为止,我想出了一切,除了如何自动调整正方形的大小以占据它们所在的div 容器的全部空间(我将 16x16 网格的形状限制为正方形,方法是将其放置在具有设置高度和宽度的 id 容器的div 中)。我尝试使用 css,将高度和宽度更改为 100%。但是,当我这样做时,它们只是出现在列中而不是网格中。
如果你在jsfiddle中打开它,并请求每边16个正方形的网格,你会看到我想要发生的事情。 每边 16 个正方形是最大值,我希望任何较少数量的正方形来填充该空间并保持正方形网格格式,而不是形成一列。我不确定该解决方案是否涉及CSS或jQuery。感谢您的任何帮助!
这是 html:
<body>
<div id ="button">
<p>Play the game</p>
</div>
<div id ="container"></div>
</div>
</body>
这是 CSS:
.squares {
background-color: #c266ff;
display: block;
height: 100%;
width: 100%;
display: inline-block;
margin-left: 3px;
}
#container {
height: 200px;
width: 470px;
margin: auto;
margin-top: 50px;
}
#button {
height: 50px;
width: 100px;
background-color: #ddcee6;
border: 2px solid #ddcee6;
border-radius: 10px;
text-align: center;
color: #19171a;
margin: auto;
margin-top: 50px;
}
下面是 jquery/javascript:
$(document).ready(function() {
$("#button").click(function() {
var x = prompt("How many squares do you want on each side of the grid? Pick between 1 and 16.");
for(var i = 0; i < (x*x); i++) {
$("<div class='squares'></div>").appendTo("#container");
}
$(".squares").one("mouseover", function() {
$(this).css("background-color","#6b00b3");
});
});
});
这里有一种方法可以计算高度和宽度,并使用jQuery应用它。
//Calculate squares height and width
var containerHeight = $('#container').innerHeight();
var containerWidth = $('#container').innerWidth();
var squareMargins = ($('.squares').outerWidth(true) - $('.squares').outerWidth()) * x; //Margin * x to take margin space into account, otherwise calculation will be off
var squareHeight = (containerHeight - squareMargins) / x;
var squareWidth = (containerWidth - squareMargins) / x;
$('.squares').height(squareHeight);
$('.squares').width(squareWidth);
在这里演示
这是一个开始。
$(document).ready(function() {
$("#button").click(function() {
var x = prompt("How many squares do you want on each side of the grid? Pick between 1 and 16.");
var s = (100 / x);
for(var i = 0; i < (x*x); i++) {
$("<div class='squares' style='width:" + s + "%; height:" + s + "%'></div>").appendTo("#container");
}
$(".squares").one("mouseover", function() {
$(this).css("background-color","#6b00b3");
});
console.log(document.body.innerHTML);
});
});
.squares {
background-color: #c266ff;
float: left;
border: 1px solid black;
box-sizing: border-box;
}
#container {
height: 200px;
width: 470px;
margin: auto;
margin-top: 50px;
}
#button {
height: 50px;
width: 100px;
background-color: #ddcee6;
border: 2px solid #ddcee6;
border-radius: 10px;
text-align: center;
color: #19171a;
margin: auto;
margin-top: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id ="button">
<p>Play the game</p>
</div>
<div id ="container">
</div>
相关文章:
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 调整窗口大小时,可拖动的对象会出现在容器外部
- ExtJS——在展开/折叠时调整面板高度
- Chrome应用程序调整窗口大小保持纵横比
- 如何自动调整标签的高度以适应内容
- 导航栏没有调整到浏览器屏幕的大小
- 调整屏幕大小后不显示子菜单
- 使用JavaScript根据窗口/视口的高度动态调整图像大小
- 合并两个数组,重新调整循环js
- Morris.js折线图x轴标签在调整大小后消失
- 调整缩放窗口高度提升缩放
- 调整屏幕大小时更改属性值
- 如何针对Android股票/本地浏览器来调整CSS
- 如何调整信息窗口的大小并点击按钮?+更改信息窗口的内容
- Adobe Air SDK在页面加载时调整窗口大小
- 更改屏幕上对象的宽度调整大小
- 客户端图像调整大小.任何已知问题
- 窗口大小调整事件在ie7中持续触发
- 手势和触摸事件 - 平滑地调整正方形的大小
- 如何创建一个网格,其中可变数量的正方形会自动调整大小以适应空间