Bak*_*yor 96 html javascript connection line jquery-ui-draggable
我如何(或哪些工具可用)在两个或多个元素之间绘制一条线来连接它们?HTML/CSS/JavaScript/SVG/Canvas的任意组合都可以.
如果您的答案支持其中任何一项,那么请提及:
此问题已更新,以巩固其中的众多变体.
Tom*_*zyk 160
jsPlumb是一个支持拖放的选项,正如其众多演示所示,包括流程图演示.
它提供免费的社区版和付费的Toolkit版.
Toolkit版本包含了一个全面的数据绑定层社区版本,以及用于构建流行库的应用程序和集成的几个UI小部件,并且已获得商业许可.
Ani*_*Ani 44
使用svgs连接线条对我来说是值得一试的,它完美地工作......首先,可缩放矢量图形(SVG)是一种基于XML的矢量图像格式,用于支持交互性和动画的二维图形.SVG图像及其行为在XML文本文件中定义.你可以使用<svg>
标签在HTML中创建一个svg .Adobe Illustrator是用于使用路径创建复杂svgs的最佳软件之一.
使用一行连接两个div的过程:
创建两个div并根据需要给它们任意位置
<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>
Run Code Online (Sandbox Code Playgroud)
(为了便于解释,我正在做一些内联样式,但为样式创建一个单独的css文件总是好的)
<svg><line id="line1"/></svg>
行标记允许我们在两个指定点(x1,y1)和(x2,y2)之间绘制一条线.(参考访问w3schools.)我们尚未指定它们.因为我们将使用jQuery来编辑行标记的属性(x1,y1,x2,y2).
在<script>
标签写
line1 = $('#line1');
div1 = $('#div1');
div2 = $('#div2');
Run Code Online (Sandbox Code Playgroud)
我使用选择器来选择两个div和行...
var pos1 = div1.position();
var pos2 = div2.position();
Run Code Online (Sandbox Code Playgroud)
jQuery position()
方法允许我们获取元素的当前位置.有关更多信息,请访问https://api.jquery.com/position/(您也可以使用offset()
方法)
现在我们已经获得了我们需要的所有位置,我们可以按如下方式绘制线条......
line1
.attr('x1', pos1.left)
.attr('y1', pos1.top)
.attr('x2', pos2.left)
.attr('y2', pos2.top);
Run Code Online (Sandbox Code Playgroud)
jQuery .attr()
方法用于更改所选元素的属性.
我们在上面所做的就是改变了行的属性
x1 = 0
y1 = 0
x2 = 0
y2 = 0
Run Code Online (Sandbox Code Playgroud)
至
x1 = pos1.left
y1 = pos1.top
x2 = pos2.left
y2 = pos2.top
Run Code Online (Sandbox Code Playgroud)
当position()
返回两个值,一个'左'和另一个'顶'时,我们可以使用.top和.left使用对象(这里是pos1和pos2)轻松访问它们...
现在,行标记有两个不同的坐标,用于在两点之间绘制线.
提示:根据需要添加事件侦听器div
提示:在脚本标记中写入任何内容之前,请确保先导入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>
Run Code Online (Sandbox Code Playgroud)
Che*_*bim 28
最近,我尝试开发一个简单的 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>
Run Code Online (Sandbox Code Playgroud)
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);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
134723 次 |
最近记录: |