Medium 1

JS draw an arrow pointing from one div to another

I have the following code:

<div id="parent">
  <div class="child" id="air1">Medium 1</div>
  <div class="child" id="glass">Medium 2</div>
  <div class="child" id="air2">Medium 1</div>
</div>

<style>
  #parent {
    background: #999;
    padding: 0px;
  }
  #glass {
    background: #666;
  }
  .child {
    background: #ccc;
    height: 200px;
    margin: 0px;
  }
</style>

I want to draw an arrow from # air1 to #glass using svg. I added the following code to the div to draw an example arrow:

<svg width="300" height="100">

    <defs>
        <marker id="arrow" markerWidth="13" markerHeight="13" refx="2" refy="6" orient="auto">
            <path d="M2,2 L2,11 L10,6 L2,2" style="fill:red;" />
        </marker>
    </defs>

    <path d="M30,150 L100,50"
          style="stroke:red; stroke-width: 1.25px; fill: none;
                 marker-end: url(#arrow);"
    />

</svg>

I do not want the arrow to point in a random direction, but I want it to point to #glass as follows: enter image description here

Also, how could I draw a less steep arrow like this: enter image description here

How can i do this?

+4
source share
1 answer

, ( svg position: absolute;) . , .

. w3schools .

#parent {
  background: #999;
  padding: 0px;
}
#glass {
  background: #666;
}
.child {
  background: #ccc;
  height: 200px;
  margin: 0px;
  position: relative;
}
svg {
  position: absolute;
  left: 0;
}
<div id="parent">
  <div class="child" id="air1">Medium 1
    <svg width="400" height="200">
      <defs>
        <marker id="arrow" markerWidth="13" markerHeight="13" refx="2" refy="6" orient="auto">
          <path d="M2,2 L2,11 L10,6 L2,2" style="fill:red;" />
        </marker>
      </defs>
      <path d="M-600,-10 L300,195" style="stroke:red; stroke-width: 1.25px; fill: none; marker-end: url(#arrow);" />
    </svg>
  </div>
  <div class="child" id="glass">Medium 2</div>
  <div class="child" id="air2">Medium 1</div>
</div>
+1

Source: https://habr.com/ru/post/1654310/


All Articles