在 html div 个元素之间画一条线

Drawing a line between html div elements

我在 w3schools 中看到了这个示例,您在其中设置了 svg 线条属性,例如 x1、y1、x2、y2,并且屏幕上出现了一条线条。我正在使用相同的方法使用 javascript 在 div 元素之间绘制 svg 线,但它似乎不起作用。

我有两个圆圈,我在 javascript 中使用 boundingClientRect 从中读取 x 和 y 值,并使用 set 属性将其设置在 svg 行上。我想要连接这两个圆圈的线

这是我的代码

const circle1 = document.querySelector('.circle--1');
const circle2 = document.querySelector('.circle--2');

const line1 = document.querySelector('#line-1');

line1.setAttribute('x1', circle1.getBoundingClientRect().x);
line1.setAttribute('y1', circle1.getBoundingClientRect().y);
line1.setAttribute('x2', circle2.getBoundingClientRect().x);
line1.setAttribute('y2', circle2.getBoundingClientRect().y);
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 62.5%;
}

body {
  font-family: sans-serif;
}

.header {
  width: 100%;
  height: 57rem;
  background-image: linear-gradient(to right, #64b5f6, #1976d2);
  padding: 0 2rem;
}

.graph {
  width: 100%;
  max-width: 120rem;
  background-color: grey;
  height: 100%;
  position: relative;
}

.circle {
  width: 5rem;
  height: 5rem;
  border-radius: 50%;
  background-color: white;
  font-size: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
}

.circle--1 {
  left: 0;
  bottom: 5%;
}

.circle--1 {
  left: 10%;
  bottom: 60%;
}

.circle--2 {
  right: 10%;
  bottom: 60%;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./style.css" />
    <title>Connect Divs</title>
  </head>
  <body>
    <header class="header">
      <div class="graph">
        <div class="circle circle--1">1</div>
        <div class="circle circle--2">2</div>

        <svg>
          <line
            id="line-1"
            style="stroke: rgb(255, 255, 255); stroke-width: 3"
          />
        </svg>
      </div>
    </header>

    <script src="./script.js"></script>
  </body>
</html>

一个 svg 元素有一个尺寸,你的线在那个区域之外。

svg 的默认设置是:

svg:not(:root) {
    overflow: hidden;
}

这样您的台词就不会显示了。

如果将此添加到您的样式中:

svg {
  overflow: visible;
  border: 1px solid red;
}

您将看到 svg 的位置及其尺寸,overflow: visible; 使线条可见:

const circle1 = document.querySelector('.circle--1');
const circle2 = document.querySelector('.circle--2');

const line1 = document.querySelector('#line-1');

line1.setAttribute('x1', circle1.getBoundingClientRect().x);
line1.setAttribute('y1', circle1.getBoundingClientRect().y);
line1.setAttribute('x2', circle2.getBoundingClientRect().x);
line1.setAttribute('y2', circle2.getBoundingClientRect().y);
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 62.5%;
}

body {
  font-family: sans-serif;
}

.header {
  width: 100%;
  height: 57rem;
  background-image: linear-gradient(to right, #64b5f6, #1976d2);
  padding: 0 2rem;
}

.graph {
  width: 100%;
  max-width: 120rem;
  background-color: grey;
  height: 100%;
  position: relative;
}

.circle {
  width: 5rem;
  height: 5rem;
  border-radius: 50%;
  background-color: white;
  font-size: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
}

.circle--1 {
  left: 0;
  bottom: 5%;
}

.circle--1 {
  left: 10%;
  bottom: 60%;
}

.circle--2 {
  right: 10%;
  bottom: 60%;
}

svg {
  overflow: visible;
  border: 1px solid red;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./style.css" />
    <title>Connect Divs</title>
  </head>
  <body>
    <header class="header">
      <div class="graph">
        <div class="circle circle--1">1</div>
        <div class="circle circle--2">2</div>

        <svg>
          <line
            id="line-1"
            style="stroke: rgb(255, 255, 255); stroke-width: 3"
          />
        </svg>
      </div>
    </header>

    <script src="./script.js"></script>
  </body>
</html>

但是您应该更改 svg.

的大小 and/or 定位而不是使用 overflow: visible;