简体   繁体   中英

How to create grid and place elements randomly inside column?

I have a responsive grid that is created with js and css. Inside each column in the grid I want to place elements (the red squares), but the squares should be placed randomly and only inside some of the columns. There is 50 columns, so let's say I want to place 20 squares randomly inside columns so that the squares can't overlap. How do I achieve this in the best way?

js

var grid = document.getElementById("grid");

for(var i = 0; i < 50; i++) {
    var square = document.createElement("div");
    square.className = 'square';
    grid.appendChild(square);

    var child = document.createElement("div");
    child.className = 'child';
    square.appendChild(child);
}

fiddle

First add an ID to each square, then the idea is to generate a random number between 0 and 49 to be able to access those squares. Each time you add a square, you have to store its index to check whether it has already been added. Only stop until 20 squares is added.

 var field = document.getElementById("field"); for (var i = 0; i < 50; i++) { var square = document.createElement("div"); square.className = 'square'; square.id = 'square' + i; field.appendChild(square); } var squaresPlaced = []; // Stores the squares index placed while (squaresPlaced.length < 20) { // Stop only if 20 squares is added var randomIndex = parseInt(49 * Math.random()); // Generate a random number between 0 and 49 // Only add the square if it doesn't exist already if (squaresPlaced.indexOf(randomIndex) === -1) { squaresPlaced.push(randomIndex); document.getElementById('square' + randomIndex).style.borderColor = 'red'; } } 
 html, body { margin: 0; height: 100%; } #field { width: 60vw; height: 60vw; margin: 0 auto; font-size: 0; position: relative; } #field>div.square { font-size: 1rem; vertical-align: top; display: inline-block; width: 10%; height: 10%; box-sizing: border-box; border: 1px solid #000; } #field>div.circle { font-size: 1rem; vertical-align: top; display: inline-block; width: 10%; height: 10%; box-sizing: border-box; border: 1px solid #f00; border-radius: 100px; } 
 <div id="field"></div> 

ANSWER UPDATE

is it possible to prevent a circle to appear next to another?

Yes, it's enough to change how to generate random numbers. Change this line:

if(arr.indexOf(randomnumber) > -1) continue;

to:

if(arr.indexOf(randomnumber) > -1 || arr.indexOf(randomnumber + 1) > -1
                    || arr.indexOf(randomnumber - 1) > -1) continue;

Also if I want to add more shapes, can I just create and append another shape and then clone that in the foreach?

Yes. I added an oval figure.

My proposal is:

  • generate 20 random different numbers
  • clone and move the circle adjusting the size
  • hide all generated circles:

 var field = document.getElementById("field"); for (var i = 0; i < 50; i++) { var square = document.createElement("div"); square.className = 'square'; field.appendChild(square); } for (var i = 0; i < 50; i++) { var circle = document.createElement("div"); circle.className = (Math.random() >= 0.5) ? 'circle' : 'oval'; field.appendChild(circle); } var arr = []; while (arr.length < 20) { var randomnumber = Math.ceil(Math.random() * 50) - 1; if (arr.indexOf(randomnumber) > -1 || arr.indexOf(randomnumber + 1) > -1 || arr.indexOf(randomnumber - 1) > -1) continue; arr[arr.length] = randomnumber; } arr.forEach(function (rnd, idx) { $('#field > .circle, #field > .removed, #field > .oval, #field > .ovalremoved').eq(rnd) .css({border: '1px solid #0000cc'}).clone() .css({width: '100%', height: '100%'}) .appendTo($('.square').eq(rnd)); }) 
 html, body { margin: 0; height: 100%; } #field { width: 60vw; height: 60vw; margin: 0 auto; font-size: 0; position: relative; } #field > div.square { font-size: 1rem; vertical-align: top; display: inline-block; width: 10%; height: 10%; box-sizing: border-box; border: 1px solid #000; } div.circle { font-size: 1rem; vertical-align: top; display: inline-block; width: 10%; height: 10%; box-sizing: border-box; border: 1px solid #f00; border-radius: 100px; } div.oval { font-size: 1rem; vertical-align: top; display: inline-block; width: 10%; height: 10%; box-sizing: border-box; border: 1px solid #f00; border-radius: 100px / 50px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="field"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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