-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrag-v1.0.html
81 lines (73 loc) · 3.01 KB
/
drag-v1.0.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>JS 基础拖曳 </title>
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<link charset="utf-8" rel="stylesheet" href="base.css" type="text/css" />
</head>
<body>
<div style="position: absolute; left: 100px; top: 100px; width: 250px; background-color: white; border: solid black;" id="J_boxDiv">
<div style="background-color: gray; border-bottom: dotted black; width: 250px; height: 30px; font-family: sans-serif; font-weight: bold;" id="J_dragBarDiv">
</div>
<div>
This is a test. Testing, testing, testing.
<p>
This is a test.
</p>Test.
</div>
</div>
<script src="src/event-v2.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var data = {
startX: 0,
startY: 0,
deltaX: 0,
deltaY: 0,
deltaX: 0,
deltaY: 0,
elementToDrag: document.getElementById("J_boxDiv"),
dragBar: document.getElementById("J_dragBarDiv")
};
var moveHandler = function(e){
data.elementToDrag.style.left = (e.clientX - data.deltaX) + "px";
data.elementToDrag.style.top = (e.clientY - data.deltaY) + "px";
// 禁止选择
if ('getSelection' in window) {
window.getSelection().removeAllRanges();
}
else {
try {
document.selection.empty();
}
catch (e) {
};
}
e.stopPropagation();
};
var upHandler = function(e){
EVENT.remove(document, "mousemove", moveHandler);
EVENT.remove(document, "mousemup", upHandler);
e.stopPropagation();
};
EVENT.add(data.dragBar, "mouseover", function(e){
e.target.style.cursor = "move";
});
EVENT.add(data.dragBar, "mouseout", function(e){
e.target.style.cursor = "default";
});
EVENT.add(data.dragBar, "mousedown", function(e){
data.startX = e.clientX;
data.startY = e.clientY;
data.origX = data.elementToDrag.offsetLeft;
data.origY = data.elementToDrag.offsetTop;
data.deltaX = data.startX - data.origX;
data.deltaY = data.startY - data.origY;
EVENT.add(document, "mousemove", moveHandler);
EVENT.add(document, "mouseup", upHandler);
});
</script>
</body>
</html>