Skip to content

Commit aa2bdfe

Browse files
committed
增加svg
1 parent e3013e8 commit aa2bdfe

3 files changed

Lines changed: 10395 additions & 0 deletions

File tree

‎SVG/index.html‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>箭头直线</title>
8+
<style type="text/css">
9+
body,html,div,svg{
10+
margin:0;
11+
padding:0;
12+
}
13+
.wrap{
14+
position:relative;
15+
height:100vh;
16+
}
17+
.itemwrap{
18+
position: absolute;
19+
width:50%;
20+
height:50%;
21+
left:100px;
22+
top:100px;
23+
}
24+
.item{
25+
position: absolute;
26+
left:100px;
27+
top:100px;
28+
border:1px solid #333;
29+
height:20px;
30+
}
31+
.lineWrap{
32+
position: absolute;
33+
left:0;
34+
top:0;
35+
overflow:visible;
36+
}
37+
</style>
38+
</head>
39+
<body>
40+
<div class="wrap">
41+
<svg>
42+
<defs>
43+
<marker id="arrow" markerUnits="strokeWidth" markerWidth="12" markerHeight="12" viewBox="0 0 12 12" refX="6" refY="6" orient="auto">
44+
<path xmlns="http://www.w3.org/2000/svg" d="M2,2 L10,6 L2,10 L6,6 L2,2" style="fill: #000000;" />
45+
</marker>
46+
</defs>
47+
<path d="M20,70 T80,100 T160,80 T200,90" fill="white" stroke="red" stroke-width="2" marker-start="url(#arrow)" marker-mid="url(#arrow)"
48+
marker-end="url(#arrow)" />
49+
</svg>
50+
<svg class="lineWrap">
51+
<line id="line" xmlns="http://www.w3.org/2000/svg" x1="0" y1="0" x2="200" y2="50" stroke="#000" stroke-width="2" marker-end="url(#arrow)"
52+
stroke-dasharray="10,10"></line>
53+
</svg>
54+
<svg>
55+
<path id="path" fill="white" stroke="red" stroke-width="2" marker-end="url(#arrow)" d="M20,20 L50,50"></path>
56+
</svg>
57+
<div class="itemwrap">
58+
<div class="item item1">item1</div>
59+
</div>
60+
<div class="item item2">item2</div>
61+
</div>
62+
<script type="text/javascript" src="jquery.js"></script>
63+
<script type="text/javascript">
64+
$(function(){
65+
66+
var pos1 = getElCoordinate($('.item1')[0])
67+
var pos2 = getElCoordinate($('.item2')[0])
68+
var start = getPos(pos1,pos2).start
69+
var end = getPos(pos1, pos2).end
70+
71+
$('#line').attr({x1: start.x, y1:start.y, x2: end.x, y2: end.y})
72+
73+
74+
$('#path').attr({d:'M20,20 L100,100'})
75+
76+
drag($('.wrap'))
77+
function getPos(pos1, pos2){
78+
//分四种情况
79+
var x1,y1,x2,y2;
80+
if(pos2.top<pos1.top){
81+
//结束点位于左上角
82+
x1 = pos1.left + pos1.width/2;
83+
y1 = pos1.top;
84+
y2 = pos2.top + pos2.height
85+
if(pos2.left<pos1.left){
86+
x2 = pos2.left + pos2.width/2
87+
}else{//右上角
88+
x2 = pos2.left + pos2.width / 2
89+
}
90+
}else{
91+
x1 = pos1.left + pos1.width / 2;
92+
y1 = pos1.top + pos1.height;
93+
x2 = pos2.left + pos2.width/2
94+
y2 = pos2.top
95+
}
96+
return {
97+
start :{x:x1,y:y1},
98+
end : {x:x2,y:y2}
99+
}
100+
}
101+
function drag(obj) {
102+
var dragEle = obj;
103+
var _move = false;//移动标记
104+
var _x, _y;//鼠标离控件左上角的相对位置
105+
dragEle.click(function () {
106+
//alert("click");//点击(松开后触发)
107+
}).mousedown(function (e) {
108+
_move = true;
109+
_x = e.pageX - parseInt(dragEle.css("left"));
110+
_y = e.pageY - parseInt(dragEle.css("top"));
111+
// dragEle.fadeTo(20, 0.9);//点击后开始拖动并透明显示
112+
});
113+
$(document).mousemove(function (e) {
114+
if (_move) {
115+
var x = e.pageX - _x;//移动时根据鼠标位置计算控件左上角的绝对位置
116+
var y = e.pageY - _y;
117+
dragEle.css({ top: y, left: x });//控件新位置
118+
}
119+
}).mouseup(function () {
120+
_move = false;
121+
dragEle.fadeTo("fast", 1);//松开鼠标后停止移动并恢复成不透明
122+
});
123+
}
124+
function getElCoordinate(dom) {
125+
var t = dom.offsetTop;
126+
var l = dom.offsetLeft;
127+
var w = dom.offsetWidth;
128+
var h = dom.offsetHeight;
129+
dom = dom.offsetParent;
130+
while (!$(dom).hasClass('wrap')) {
131+
t += dom.offsetTop;
132+
l += dom.offsetLeft;
133+
dom = dom.offsetParent;
134+
}; return {
135+
top: t,
136+
left: l,
137+
width:w,
138+
height:h
139+
};
140+
}
141+
142+
143+
144+
145+
146+
})
147+
</script>
148+
</body>
149+
</html>

0 commit comments

Comments
 (0)