如何在两个或多个元素之间绘制一条连接线?可以使用HTML/CSS/JavaScript/SVG/Canvas的任意组合。
如果你的答案支持以下任何一种,请注明:
- 可拖动元素
- 可拖动/可编辑连接
- 避免元素重叠
该问题已更新以合并众多变体。
如何在两个或多个元素之间绘制一条连接线?可以使用HTML/CSS/JavaScript/SVG/Canvas的任意组合。
如果你的答案支持以下任何一种,请注明:
该问题已更新以合并众多变体。
对我来说,使用SVG连接线是值得一试的,而且它完美地起作用了...首先,可缩放矢量图形(SVG)是一种基于XML的二维图形矢量图像格式,支持交互和动画。SVG图像及其行为在XML文本文件中定义。您可以使用<svg>
标签在HTML中创建SVG。Adobe Illustrator是用于使用路径创建复杂SVG的最佳软件之一。
连接两个div以形成一条线的过程:
create two divs and give them any position as you need
<div id="div1" style="width: 100px; height: 100px; top:0; left:0; background:#e53935 ; position:absolute;"></div>
<div id="div2" style="width: 100px; height: 100px; top:0; left:300px; background:#4527a0 ; position:absolute;"></div>
(for the sake of explanation I am doing some inline styling but it is always good to make a separate css file for styling)
<svg><line id="line1"/></svg>
Line tag allows us to draw a line between two specified points(x1,y1) and (x2,y2). (for a reference visit w3schools.) we haven't specified them yet. because we will be using jQuery to edit the attributes (x1,y1,x2,y2) of line tag.
in <script>
tag write
line1 = $('#line1');
div1 = $('#div1');
div2 = $('#div2');
I used selectors to select the two divs and line...
var pos1 = div1.position();
var pos2 = div2.position();
jQuery position()
method allows us to obtain the current position of an element. For more information, visit https://api.jquery.com/position/ (you can use offset()
method too)
line1
.attr('x1', pos1.left)
.attr('y1', pos1.top)
.attr('x2', pos2.left)
.attr('y2', pos2.top);
jQuery的.attr()
方法用于更改所选元素的属性。
我们在上述行中所做的所有事情都是从一行中更改属性。
x1 = 0
y1 = 0
x2 = 0
y2 = 0
为了
x1 = pos1.left
y1 = pos1.top
x2 = pos2.left
y2 = pos2.top
由于position()
返回两个值,一个是'left'(左边),另一个是'top'(顶部),我们可以使用对象(此处为pos1和pos2)轻松访问它们的.top和.left属性...
现在,线标记具有绘制两个点之间的线所需的两个不同坐标。
提示:根据需要向div添加事件侦听器
提示:确保在编写script标记中的任何内容之前首先导入jQuery库
在通过JQuery添加坐标后...它看起来像这样
以下代码片段仅用于演示目的,请按照以上步骤获取正确的解决方案。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div1" style="width: 100px; height: 100px; top:0; left:0; background:#e53935 ; position:absolute;"></div>
<div id="div2" style="width: 100px; height: 100px; top:0; left:300px; background:#4527a0 ; position:absolute;"></div>
<svg width="500" height="500"><line x1="50" y1="50" x2="350" y2="50" stroke="red"/></svg>
最近,我尝试开发一个简单的Web应用程序,使用拖放组件,并连接它们之间的线条。我找到了这两个简单而神奇的JavaScript库:
工作示例链接(用法:点击添加场景以创建可拖动元素,点击添加选择以在两个不同的可拖动元素之间绘制引导线)
几天前,我也有同样的需求
我使用了一个全宽和全高的svg,并在所有div下方添加了它,动态地向这些svg添加了线条。
查看我如何使用svg实现这一点,在此处查看:
HTML
<div id="ui-browser"><div class="anchor"></div>
<div id="control-library" class="library">
<div class="name-title">Control Library</div>
<ul>
<li>Control A</li>
<li>Control B</li>
<li>Control C</li>
<li>Control D</li>
</ul>
</div><!--
--></div><!--
--><div id="canvas">
<svg id='connector_canvas'></svg>
<div class="ui-item item-1"><div class="con_anchor"></div></div>
<div class="ui-item item-2"><div class="con_anchor"></div></div>
<div class="ui-item item-3"><div class="con_anchor"></div></div>
<div class="ui-item item-1"><div class="con_anchor"></div></div>
<div class="ui-item item-2"><div class="con_anchor"></div></div>
<div class="ui-item item-3"><div class="con_anchor"></div></div>
</div><!--
--><div id="property-browser"></div>
https://jsfiddle.net/kgfamo4b/
$('.anchor').on('click',function(){
var width = parseInt($(this).parent().css('width'));
if(width==10){
$(this).parent().css('width','20%');
$('#canvas').css('width','60%');
}else{
$(this).parent().css('width','10px');
$('#canvas').css('width','calc( 80% - 10px)');
}
});
$('.ui-item').draggable({
drag: function( event, ui ) {
var lines = $(this).data('lines');
var con_item =$(this).data('connected-item');
var con_lines = $(this).data('connected-lines');
if(lines) {
lines.forEach(function(line,id){
$(line).attr('x1',$(this).position().left).attr('y1',$(this).position().top+1);
}.bind(this));
}
if(con_lines){
con_lines.forEach(function(con_line,id){
$(con_line).attr('x2',$(this).position().left)
.attr('y2',$(this).position().top+(parseInt($(this).css('height'))/2)+(id*5));
}.bind(this));
}
}
});
$('.ui-item').droppable({
accept: '.con_anchor',
drop: function(event,ui){
var item = ui.draggable.closest('.ui-item');
$(this).data('connected-item',item);
ui.draggable.css({top:-2,left:-2});
item.data('lines').push(item.data('line'));
if($(this).data('connected-lines')){
$(this).data('connected-lines').push(item.data('line'));
var y2_ = parseInt(item.data('line').attr('y2'));
item.data('line').attr('y2',y2_+$(this).data('connected-lines').length*5);
}else $(this).data('connected-lines',[item.data('line')]);
item.data('line',null);
console.log('dropped');
}
});
$('.con_anchor').draggable({drag: function( event, ui ) {
var _end = $(event.target).parent().position();
var end = $(event.target).position();
if(_end&&end)
$(event.target).parent().data('line')
.attr('x2',end.left+_end.left+5).attr('y2',end.top+_end.top+2);
},stop: function(event,ui) {
if(!ui.helper.closest('.ui-item').data('line')) return;
ui.helper.css({top:-2,left:-2});
ui.helper.closest('.ui-item').data('line').remove();
ui.helper.closest('.ui-item').data('line',null);
console.log('stopped');
}
});
$('.con_anchor').on('mousedown',function(e){
var cur_ui_item = $(this).closest('.ui-item');
var connector = $('#connector_canvas');
var cur_con;
if(!$(cur_ui_item).data('lines')) $(cur_ui_item).data('lines',[]);
if(!$(cur_ui_item).data('line')){
cur_con = $(document.createElementNS('http://www.w3.org/2000/svg','line'));
cur_ui_item.data('line',cur_con);
} else cur_con = cur_ui_item.data('line');
connector.append(cur_con);
var start = cur_ui_item.position();
cur_con.attr('x1',start.left).attr('y1',start.top+1);
cur_con.attr('x2',start.left+1).attr('y2',start.top+1);
});
没有拖放的固定示例:
没有拖放功能的交互示例:
拖放示例:
这个答案基于Glenn Dayton的回答。要创建连接,可以使用edgehandles扩展。它还不支持编辑现有的连接。问题。
要编辑连接形状,可以使用edge-editing扩展。演示。
edit-editation 扩展看起来很有前途,但目前还没有演示。