如何在所有元素 html 上画线

我想画一条线连接两个 div,我使用了一个绝对位置的 svg,但是两个 div 之间的按钮无法点击。

我怎么能用z-index max,绝对位置画一条线,它可以点击这条线下的元素。 如果您有任何想法,它将对我有很大帮助,谢谢! this is my example

我的代码:

<html>
<body>
<div style="border: solid; width: 150px;height: 150px;"> div1 </div>
<svg height="210" width="500" style="position: absolute;">
  <line x1="50" y1="0" x2="200" y2="200" style="stroke:rgb(255,0);stroke-width:2" />
</svg>
<div style="border: #3F3F3F;width: 150px;height: 150px;display: flex;align-items: center;justify-content: flex-end;">
 <button>button </button>
</div>
<div style="border: solid;width: 150px;height: 150px;top: 360px;position: absolute;left: 169px;"> div3 </div>
</body>
</html>```
yunmengyi 回答:如何在所有元素 html 上画线

  1. 您可以将 pointer-events:none 添加到 svg 元素。

  2. 使用高度为 1px 的转换 div 而不是 svg 可能更好:

.container {
  position: relative;
}

.rect {
  width:200px;
  height:200px;
  position:absolute;
  border: solid 1px black;
  box-sizing: border-box;
}

#rect2 {
  left:350px;
  top: 400px;
}

.line {
  width: 400px;
  height:1px;
  background:red;
  position:absolute;
  transform:rotate(30deg);
  transform-origin:0 0;
  top:200px;
  left:100px;
}

button {
  position: absolute;
  top:280px;
  left: 220px;
}
<div class="container">
  <button>Click me</button>
  <div class="rect" id="rect1"></div>
  <div class="rect" id="rect2"></div>
  <div class="line"></div>
</div>

,

尝试赋予 SVG 元素 z-index:-1,按钮将被点击

只需将以下代码添加到您的 CSS

svg{
  z-index: -1;
}
,

添加按钮样式 -> position:fixed

本文链接:https://www.f2er.com/1376.html

大家都在问