Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
connecting divs with svg line
  for (el of chart.children) {
    i++
    previous__element = chart.children[i - 1]
    if (el.classList.contains('rule')) {
      //pass 

    } else {
      line = el.children[0].children[0]

      pos1 = previous__element.children[2].getBoundingClientRect()

      position1 = {
        top: pos1.top,
        left: pos1.left,
      }

      pos2 = el.children[2].getBoundingClientRect()

      console.log(previous__element.children[2])
      console.log(el.children[2])

      position2 = {
        top: pos2.top,
        left: pos2.left,
      }

      line.setAttribute('x1', Math.trunc(pos1.left))
      line.setAttribute('y1', Math.trunc(pos1.top))
      line.setAttribute('x2', Math.trunc(pos1.left))
      line.setAttribute('y2', Math.trunc(pos1.top))
      line.setAttribute('stroke', 'white')
    }
  }

html that gets output:

Text

For some reason this does not actually show the lines, when hovering over them in dev tools it shows the height and width is 0. I'm trying to get the line to connect to the markers in the elements.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In this example I use position relative/absolute on all the elements. I don't know if that fits your solution, but the core of the example is that the SVG document is in the background of all the boxes. So, all the lines could be placed in that one SVG document.

Maybe the reason why your lines are not showing up is that they mis the stroke-width or that your SVG element does not have a width and a height.

let chart = document.querySelector('#chart');
let poschart = chart.getBoundingClientRect();

let line = chart.querySelector('svg line');
let boxs = chart.querySelectorAll('div.box');

let pos1 = boxs[0].getBoundingClientRect();
line.setAttribute('x1', pos1.x+pos1.width/2-poschart.x);
line.setAttribute('y1', pos1.y+pos1.height/2-poschart.y);

let pos2 = boxs[1].getBoundingClientRect();
line.setAttribute('x2', pos2.x+pos2.width/2-poschart.x);
line.setAttribute('y2', pos2.y+pos2.height/2-poschart.y);
#chart {
  position: relative;
  width: 400px;
  height: 300px;
  margin: 10px 20px;
  border: thin solid black;
}

#chart svg {
  position: absolute;
}

.box {
  position: absolute;
  padding: .5em;
  border: thin solid black;
  background-color: white;
}
<div id="chart">
  <svg viewBox="0 0 400 300" width="100%" height="100%">
    <line stroke="black" stroke-width="2"/>
  </svg>
  <div class="box" style="left:50px;top:180px">Box 1</div>
  <div class="box" style="left:310px;top:100px">Box 2</div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda