下面是一个简单的HTML页面,其中包含两个点和一条用jQuery添加的连接点的线.

结果是这条线没有连接点,而是出于某种原因放置了具有一定偏移量的线.

function drawLine(){

    const line_ = $("<div>", { class: "line" });
    const p1 = $("#point1");
    var p1T = p1.position().top;
    var p1L = p1.css("left");

    // set line top equal to point1 top
    var lineT = p1T + "px";

    line_.css ({
        width: length + "px",
        transform: `rotate(${angle}rad)`,
        top: lineT,
        left: p1L
    });

    p1.parent().append(line_);
 }

// Get the elements representing the two points you want to draw a line between
 const point1 = document.getElementById('point1');
 const point2 = document.getElementById('point2');
 
 // Calculate the coordinates of the two points
 const point1Rect = point1.getBoundingClientRect();
 const point2Rect = point2.getBoundingClientRect();
 
 // Calculate the length and angle of the line
 const length = Math.sqrt((point2Rect.left - point1Rect.left) ** 2 + (point2Rect.top - point1Rect.top) ** 2);
 const angle = Math.atan2(point2Rect.top - point1Rect.top, point2Rect.left - point1Rect.left);

 drawLine();

 
#line-container {
  position: relative;
  border: 1px solid blue;
}

.line {
  position: absolute;
  height: 2px;
  background-color: aqua;
  margin: 0px;
}

.point{
  position: absolute; 
  margin: 0px;
  
}

#point1{
  background-color: red;
  top: 100px; 
  left: 100px; 
  width: 10px; 
  height: 10px;
}
#point2{
  background-color: blue;
  top: 200px; 
  left: 300px; 
  width: 10px; 
  height: 10px;
}
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <div id="line-container">
    <div id="point1" class="point"></div>
    <div id="point2" class="point"></div>
  </div>

这条线和两个点都是position: absolute;,所以topleft是相对于容器的.

对于所有3个,margin也设置为0

线TOP被设置为Point1的顶部,但线位于Point1上方.

为什么会这样呢?

推荐答案

直线围绕中心旋转,而您希望它根据原点旋转,在本例中原点为top left,可能所有其他点也是如此.所以需要加transform-origin: top left

function drawLine() {

  const line_ = $("<div>", {
    class: "line"
  });
  const p1 = $("#point1");
  var p1T = p1.position().top;
  var p1L = p1.position().left;

  // set line top equal to point1 top
  var lineT = p1T + "px";

  line_.css({
    width: length + "px",
    transform: `rotate(${angle}rad)`,
    "transform-origin": "top left",
    top: lineT,
    left: p1L
  });

  p1.parent().append(line_);
}

// Get the elements representing the two points you want to draw a line between
const point1 = document.getElementById('point1');
const point2 = document.getElementById('point2');

// Calculate the coordinates of the two points
const point1Rect = point1.getBoundingClientRect();
const point2Rect = point2.getBoundingClientRect();

// Calculate the length and angle of the line
const length = Math.sqrt((point2Rect.left - point1Rect.left) ** 2 + (point2Rect.top - point1Rect.top) ** 2);
const angle = Math.atan2(point2Rect.top - point1Rect.top, point2Rect.left - point1Rect.left);

drawLine();
#line-container {
  position: relative;
  border: 1px solid blue;
}

.line {
  position: absolute;
  height: 2px;
  background-color: aqua;
  margin: 0px;
}

.point {
  position: absolute;
  margin: 0px;
}

#point1 {
  background-color: red;
  top: 100px;
  left: 100px;
  width: 10px;
  height: 10px;
}

#point2 {
  background-color: blue;
  top: 200px;
  left: 300px;
  width: 10px;
  height: 10px;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="line-container">
  <div id="point1" class="point"></div>
  <div id="point2" class="point"></div>
</div>

Html相关问答推荐

XPATH text()函数遇到困难

使用网格时图像溢出容器高度

防止SVG边框半径zoom

CURL邮箱中的图像不能调整其大小

SCSS动画错误:心脏在页面刷新时启动动画,原因是:Checked和:Not(:Checked) Select 器

类型';联系人组件';上不存在属性';name FormControl';

我无法动态嵌入Instagram帖子,因为无法分配Instagram固定链接

有没有一种方法可以很容易地将这个div从底部:0转换到顶部:0?

文本幻灯片显示动画

如何将文本环绕心形(而不仅仅是圆形)?

带有下拉菜单的完整css导航-链接不起作用?

如何在css中在span中绘制圆弧?

无法在 CSS 中将 h1 标签居中

理解图像与其内容框之间的关系

如何从 div 内的图像中删除填充而不从其他所有内容中删除填充?

表单中如何分别禁用/启用多个提交按钮?

什么没有 margin-right 和 width:100% 溢出子元素到左边?

父背景仅在子元素中可见

我用 ejs 修复了一个奇怪的语法错误,我想知道问题出在哪里

变换元素以适应高度(Angular,SCSS)