Skip to content

Commit fa082bc

Browse files
committed
修改说明
1 parent ddcb2a3 commit fa082bc

2 files changed

Lines changed: 102 additions & 7 deletions

File tree

‎SVG/README.md‎

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,107 @@
2020
<line x1="0" y1="0" x2="200" y2="50" stroke="#000" stroke-width="2" marker-end="url(#arrow)"stroke-dasharray="10,10"></line>
2121
```
2222
>
23-
#### 2.确定起始中止两个点位置
23+
#### 2.确定起始中止两个点左上角的位置
2424
>
25-
需要连接的两个div及SVG都相对于某一个div.wrap绝对定位,
25+
需要连接的两个div及SVG都相对于某一个div.wrap绝对定位,可以先求得两个div左上角相对与div.wrap坐标:
26+
```
27+
//获取元素左上角相对于某一元素的的位置
28+
function getElCoordinate(dom) {
29+
var t = dom.offsetTop;
30+
var l = dom.offsetLeft;
31+
var w = dom.offsetWidth;
32+
var h = dom.offsetHeight;
33+
dom = dom.offsetParent;
34+
while (!$(dom).hasClass('wrap')) {
35+
t += dom.offsetTop;
36+
l += dom.offsetLeft;
37+
dom = dom.offsetParent;
38+
}; return {
39+
top: t,//Y轴坐标
40+
left: l,//X轴坐标
41+
width:w,//元素宽度
42+
height:h//元素高度
43+
};
44+
}
45+
})
46+
```
47+
>
48+
#### 3.确定连接点在div上的位置:
49+
>
50+
分为两种情况:终点div位于起点div上方,终点div位于起点div下方。
51+
```
52+
var pos1 = getElCoordinate($('.item1')[0])//起点div的位置
53+
var pos2 = getElCoordinate($('.item2')[0])//终点div的位置
54+
function getPos(pos1, pos2){
55+
//分两种情况
56+
var x1,y1,x2,y2;
57+
if(pos2.top<pos1.top){
58+
x1 = pos1.left + pos1.width/2;
59+
y1 = pos1.top;
60+
y2 = pos2.top + pos2.height
61+
if(pos2.left<pos1.left){
62+
x2 = pos2.left + pos2.width/2
63+
}else{
64+
x1 = pos1.left + pos1.width / 2;
65+
y1 = pos1.top + pos1.height;
66+
x2 = pos2.left + pos2.width/2
67+
y2 = pos2.top
68+
}
69+
return {
70+
start :{x:x1,y:y1},
71+
end : {x:x2,y:y2}
72+
}
73+
}
74+
```
75+
>
76+
#### 4.确定起止点位置后连线。
77+
>
78+
```
79+
function move(){
80+
var pos1 = getElCoordinate($('.item1')[0])
81+
var pos2 = getElCoordinate($('.item2')[0])
82+
var start = getPos(pos1, pos2).start
83+
var end = getPos(pos1, pos2).end
84+
85+
$('#line').attr({ x1: start.x, y1: start.y, x2: end.x, y2: end.y })
86+
$('#path').attr({ d: 'M20,20 L100,100' })
87+
}
88+
move()
89+
```
90+
>
91+
#### 5.两个div分别拖动并保持线始终连接
92+
>
93+
```
94+
drag($('.item'), move)
95+
function drag(obj,callback) {
96+
var dragEles = obj;
97+
dragEles.each(function(index, dragEleDom){
98+
var _move = false;//移动标记
99+
var _x, _y;//鼠标离控件左上角的相对位置
100+
var dragEle = $(dragEleDom)
101+
dragEle.click(function () {
102+
//alert("click");//点击(松开后触发)
103+
}).mousedown(function (e) {
104+
_move = true;
105+
_x = e.pageX - parseInt(dragEle.css("left"));
106+
_y = e.pageY - parseInt(dragEle.css("top"));
107+
// dragEle.fadeTo(20, 0.9);//点击后开始拖动并透明显示
108+
});
109+
$(document).mousemove(function (e) {
110+
if (_move) {
111+
var x = e.pageX - _x;//移动时根据鼠标位置计算控件左上角的绝对位置
112+
var y = e.pageY - _y;
113+
dragEle.css({ top: y, left: x });//控件新位置
114+
if(callback){callback()}
115+
}
116+
}).mouseup(function () {
117+
_move = false;
118+
dragEle.fadeTo("fast", 1);//松开鼠标后停止移动并恢复成不透明
119+
});
120+
})
121+
122+
}
123+
```
26124
>
125+
#### 梳理下流程:分析需求>确定使用SVG>找到连接点>连线>拖动时保持连线。
126+
#### 源码托管于[github](https://github.com/liubin915249126/javascript/tree/master/SVG)欢迎star

‎SVG/index.html‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@
147147
height:h
148148
};
149149
}
150-
151-
152-
153-
154-
155150
})
156151
</script>
157152
</body>

0 commit comments

Comments
 (0)