Javascript 要使用动态网格展开的Div容器
这有点像是两个部分。我有一个由div(.unit)组成的动态网格,我试图将其包含在一个更大的div(#托盘)中。网格根据用户在提示中给出的整数增加。我的问题是,在不使用绝对定位的情况下,如何使较大的div相对于网格的大小进行扩展?此外,网格不会减少到用户给定的值,它只会增加。任何帮助都将不胜感激Javascript 要使用动态网格展开的Div容器,javascript,jquery,html,css,grid,Javascript,Jquery,Html,Css,Grid,这有点像是两个部分。我有一个由div(.unit)组成的动态网格,我试图将其包含在一个更大的div(#托盘)中。网格根据用户在提示中给出的整数增加。我的问题是,在不使用绝对定位的情况下,如何使较大的div相对于网格的大小进行扩展?此外,网格不会减少到用户给定的值,它只会增加。任何帮助都将不胜感激 $(document).ready(function() { var userChoice = 10; for(var x = 0; x < userChoice; x++) {
$(document).ready(function() {
var userChoice = 10;
for(var x = 0; x < userChoice; x++) {
for(var y = 0; y < userChoice; y++) {
var unit = $("<div class='unit'></div>");
unit.appendTo('#pallet'); //puts div "unit" into div "pallet" using appendTo
}
}
$(".unit").hover(function(){ //jquery selects the "unit" div and issues a function on it to change the css
$(this).css("background-color", "black"); //this is in reference to .unit changing the css to black
});
document.getElementById("reset").onclick = gridReset; //reset is the id of our button, so when it's clicked it will call our function gridReset
function gridReset() { //function grid reset first asks a prompt then stores the user's choice value in a variable and puts it through checks to see if its valid
userChoice = prompt("Between 1 and 64 how big would you like your grid?"); //we put our user choice in the function so as to not call a prompt at the start of the page load AND to store the variable in the scope
if (isNaN(userChoice)) {
alert(userChoice + " is not a number");
}
else if (userChoice >= 65 || userChoice <= 0) {
alert("Choose a number between 1 and 64");
}
else {
$(".unit").empty();
$(".unit").css("background-color", "blue");
for(var x = 0; x < userChoice; x++) { //for loop creates iteration of userChoice
for(var y = 0; y < userChoice; y++) { //for loop creates iteration of userChoice
var unit = $("<div class='unit'></div>"); //creates a variable "unit" = to a jquery div element with a class of unit
unit.appendTo('#pallet'); //puts div "unit" into div "pallet" using appendTo
$(".unit").hover(function(){ //jquery selects the "unit" div and issues a function on it to change the css
$(this).css("background-color", "black"); //this is in reference to
.unit changing the css to black
});
}
}
}
}
});
你所说的
是什么意思?网格不会缩小到用户给定的值,它只会增加?一旦提示用户提交一个整数,userchoice值不会缩小网格超过初始值10。我假设它将使用(“.unit”)。空块,但div仍然存在
<!DOCTYPE html>
<html>
<head>
<title>Odin Div Project</title>
<link rel="stylesheet" type="text/css" href="styles/styles.css" />
</head>
<body>
<button id="reset" type="button">Reset Grid</button>
<div id="pallet">
</div>
<script type="text/javascript" src="scripts/jquery-3.2.0.min.js">
</script>
<script type="text/javascript" src="scripts/script.js"> </script>
</body>
</html>
#pallet {
position: relative;
left: 425px;
top: 150px;
background-color: black;
height: 440px;
width: 440px;
}
.unit {
position: relative;
background-color: blue;
height: 20px;
width: 20px;
margin: 1px;
display: inline-block;
float: left;
}