Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

278
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!