Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
How can I recognize the vertical empty spaces in my markup in JavaScript?

in this HTML file, there are a couple of divs that are absolute

I want to write a code in JavaScript that can tell me the row of each div. Whenever in vertical axes we are no box there is a row, for me problem is how can I tell my application "there is a row"!

here my HTML code:

<html>
<head>
    <style>
        .container {
            width    : 1170px;
            margin   : 0 auto;
            position : relative;
        }

        .box {
            position   : absolute;
            background : red;
            width      : 300px;
            height     : 300px;
            color      : #fff;
            font-size  : 50px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="box" style="top: 20px; left: 30px" id="box1">box1</div>
    <div class="box" style="height:100px; top: 90px; left: 530px" id="box2">box2</div>
    <div class="box" style="top: 1450px; left: 230px" id="box3">box3</div>
    <div class="box" style="top: 550px; left: 630px" id="box4">box4</div>
    <div class="box" style="top: 950px; left: 130px" id="box5">box5</div>
</div>


</body>

</html>

I want my code to work with more or fewer boxes so I think I should write a function but without understanding these rows I can't do that... Any advice?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here is a simple example, first it find the rows min and max heights and then loops through all boxes and if the top border height is between a row top and bottom height it assigns that box to that row, and you will get an object with your boxes ids and the rows they are in: (rows numbers starts at 0, i used the array position to get it, but you can assign any number you want when creating the rows array ) here is a sample output:

[
  {
    "box": "box1",
    "row": 0
  },
  {
    "box": "box2",
    "row": 0
  },
  {
    "box": "box3",
    "row": 3
  },
  {
    "box": "box4",
    "row": 1
  },
  {
    "box": "box5",
    "row": 2
  }
]

let boxes = document.getElementsByClassName("box");
// construct an array with the rows
function getRow(fromHeight) {
  const o = {};
  for (var i = 0; i < boxes.length; i++) {
    const boxRect = boxes[i].getBoundingClientRect();
    if ((!o.top || o.top > boxRect.top) && boxRect.top > fromHeight) {
      o.top = boxRect.top;
      o.bottom = boxRect.bottom;
    }
  }
  return o;
}
let rows = [];
let fromHeight = 0;
for (var i = 0; i < boxes.length; i++) {
  let row = getRow(fromHeight);
  if (Object.keys(row).length !== 0) rows.push(row);
  fromHeight = row.bottom;
}
// console.log(rows);

// get array with every box row
let array = [];
for (var i = 0; i < boxes.length; i++) {
  const boxRect = boxes[i].getBoundingClientRect();
  var row = rows.findIndex(p => p.top <= boxRect.top && p.bottom >= boxRect.top);
  array.push({box:boxes[i].id, row: row});
}
console.log(array);
<html>

<head>
  <style>
    .container {
      width: 1170px;
      margin: 0 auto;
      position: relative;
    }

    .box {
      position: absolute;
      background: red;
      width: 300px;
      height: 300px;
      color: #fff;
      font-size: 50px;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="box" style="top: 20px; left: 30px" id="box1">box1</div>
    <div class="box" style="height:100px; top: 90px; left: 530px" id="box2">box2</div>
    <div class="box" style="top: 1450px; left: 230px" id="box3">box3</div>
    <div class="box" style="top: 550px; left: 630px" id="box4">box4</div>
    <div class="box" style="top: 950px; left: 130px" id="box5">box5</div>
  </div>

</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda