【发布时间】:2021-02-11 00:53:38
【问题描述】:
我有一个 div 需要使用线条和框(将包含一条消息)来识别,如下面的模型图像所示。2 和 3(线条和矩形框)相互固定并可拖动,1 (线)可以向任何方向拉伸。我已经创建了这个盒子,但我无法弄清楚如何在它上面附加一条线。这是我尝试过的。
js
const $b1 = $("#box1");
const $b2 = $("#box2");
const $line = $("#line");
const coordinates = function() {
debugger;
const x1 = $b1.offset().left;
const y1 = $b1.offset().top + $b1.height()/2;
const x2 = $b2.offset().left + $b1.width()/2;
const y2 = $b2.offset().top + $b1.height()/2;
moveLine(x1, y1, x2, y2);
}
coordinates();
function moveLine(x1, y1, x2, y2) {
var length = Math.sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
var angle = Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI;
var transform = 'rotate(' + angle + 'deg)';
offsetX = (x1 > x2) ? x2 : x1;
offsetY = (y1 > y2) ? y2 : y1;
$line.css({
'position': 'absolute',
'-webkit-transform': transform,
'-moz-transform': transform,
'transform': transform
})
.width(length)
.offset({
left: offsetX,
top: offsetY
});
}
$('#box1').draggable({
drag: coordinates
});
HTML
<div class="box" id="box1">10%</div>
<p id="box2">www.google.com</p>
<div class="line" id="line"></div>
css
.box {
border: 1px solid black;
background-color: #ffffff;
width: 100px;
height: 40px;
position: absolute;
}
#line1 {
top: 100px;
left: 50px;
/*transform: rotate(222deg);
-webkit-transform: rotate(222deg);
-ms-transform: rotate(222deg);*/
}
.line {
width: 1px;
height: 1px;
background-color: black;
position: absolute;
z-index: -1; /* put line behind the boxes */
}
#box1 {
top: 150px;
left: 150px;
}
#box2 {
top: 200px;
left: 200px;
position:relative;
}
【问题讨论】:
-
工具提示在哪里?
-
一二三的盒子
标签: javascript html jquery plugins