【问题标题】:Drawing a line between html div elements在 html div 元素之间画一条线
【发布时间】:2021-09-13 09:31:10
【问题描述】:

我在 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>

【问题讨论】:

  • 如果您知道 w3schools 示例,您可以链接它。
  • 在开发工具中检查您的示例表明,该行实际上设置了正确的 x/y 值(至少一半) - 但该行本身不可见,因为它位于 SVG 的“外部”首先。您没有为 SVG 指定任何宽度或高度,也没有指定视图框 - 因此它位于文档顶部,默认尺寸为 300 * 150 像素,甚至没有向下延伸到两个圆圈所在的位置。 (将svg { background:red; } 添加到您的样式表中,您将很容易明白我的意思。)

标签: javascript html css svg


【解决方案1】:

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 的大小和/或位置,而不是使用overflow: visible;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 2014-03-18
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    相关资源
    最近更新 更多