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

220
Visualizações
How to get all sibling div's inside overlapping div area?

I have a large div and smaller siblings divs positioned inside it like this:

.large{
  height:20rem;
  width:20rem;
  background-color:red;
  position:absolute;
}

.item1{
  height:5rem;
  width:5rem;
  background-color:blue;
  top:1rem;
  position:absolute;
}

.item2{
  height:5rem;
  width:5rem;
  background-color:green;
  top:3rem;
  left:2rem;
  position:absolute;
}

.item3{
  height:5rem;
  width:5rem;
  background-color:yellow;
  top:1rem;
  left:6rem;
  position:absolute;
}
<div class="large"></div>
<div class="item1"></div>
<div class="item2"></div>
<div class="item3"></div>

How do I get all the small divs within the large div dimensions?
Is there something similar to elementsFromPoint? Maybe something like elementsFromArea

Edit:

assume .large spans 320 pixels x 320 pixels
and I have multiple smaller divs on my screen, which can either be overlapping .large or outside it

How do I find divs which are overlapping .large?

Maybe we could get the position of .large & we already have the height and width of it and add it to some function like this:

elementsFromArea(large_x,large_y,large_height,large_width);

This should return an array of all the divs within that given range

(.large is merely for reference sake, I simply want to pass any given square area & find all the divs lying within it )

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

0

You can use getBoundingClientRect to find the left, right, top and bottom bounds of each element.

Then test whether there is overlap with the large element by seeing whether the left is to the left of the right side of the large element and so on:

if ( ((l <= Right) && (r >= Left)) && ( (t <= Bottom) && (b >= Top)) )

To give a more thorough test, in this snippet the blue element has been pushed down so it only partially overlaps the large one and the yellow element doesn't overlap at all.

const large = document.querySelector('.large');
const largeRect = large.getBoundingClientRect();
const Left = largeRect.left;
const Right = largeRect.right;
const Top = largeRect.top;
const Bottom = largeRect.bottom;
const items = document.querySelectorAll('.large ~ *');
let overlappers = [];
items.forEach(item => {
  const itemRect = item.getBoundingClientRect();
  const l = itemRect.left;
  const r = itemRect.right;
  const t = itemRect.top;
  const b = itemRect.bottom;
  if (((l <= Right) && (r >= Left)) && ((t <= Bottom) && (b >= Top))) {
    overlappers.push(item);
  }
});
console.log('The items with these background colors overlap the large element:');
overlappers.forEach(item => {
  console.log(window.getComputedStyle(item).backgroundColor);
});
.large {
  height: 20rem;
  width: 20rem;
  background-color: red;
  position: absolute;
}

.item1 {
  height: 5rem;
  width: 5rem;
  background-color: blue;
  top: 19rem;
  position: absolute;
}

.item2 {
  height: 5rem;
  width: 5rem;
  background-color: green;
  top: 3rem;
  left: 2rem;
  position: absolute;
}

.item3 {
  height: 5rem;
  width: 5rem;
  background-color: yellow;
  top: 1rem;
  left: 26rem;
  position: absolute;
}
<div>
  <div class="large"></div>
  <div class="item1"></div>
  <div class="item2"></div>
  <div class="item3"></div>
</div>

Note, this snippet tests only those elements which are siblings of large in the CSS sense, that is that follow large. If you want all siblings whether they follow large or come before it then go back up to large's parent and get all its children (which will of course include large).

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