-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag.js
60 lines (51 loc) · 1.86 KB
/
drag.js
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
function dragElement( subject ) { /** Move selected element anywhere on page */
// See init for required initialization
var x1, y1, x2, y2, dx;
if( document.getElementById( subject.id + "header" ) ) {
// if present, the header is where you move the DIV from:
document.getElementById( subject.id + "header" ).onmousedown = dragMouseDown;
dx = 0;
} else {
// otherwise, move the DIV from anywhere inside the DIV:
subject.onmousedown = dragMouseDown;
dx = 0;
}
function dragMouseDown( e ) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
x2 = e.clientX;
y2 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag( e ) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
x1 = x2 - e.clientX;
y1 = y2 - e.clientY;
x2 = e.clientX;
y2 = e.clientY;
// set the element's new position:
subject.style.top = ( subject.offsetTop - y1 - dx ) + "px";
subject.style.left = ( subject.offsetLeft - x1 - dx ) + "px";
}
function closeDragElement() { // stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
if( ! context.dragables ) {
context.dragables = {};
}
var itm = subject.id;
if( ! context.dragables[ itm ] ) {
context.dragables[ itm ] = {};
}
context.dragables[ itm ].left = x2; // remember position and size
context.dragables[ itm ].top = y2;
context.dragables[ itm ].width = subject.offsetWidth;
context.dragables[ itm ].height = subject.offsetHeight;
saveContext( itm );
}
}