-
Notifications
You must be signed in to change notification settings - Fork 0
/
draggable.js
50 lines (38 loc) · 1.04 KB
/
draggable.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
$('document').ready(function(){
drag($("#eyes"));
drag($("#nose"));
drag($("#tBody"));
drag($("#fur"));
drag($("#ears"));
});
selectedElement = null;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
initialX = e.clientX;
initialY = e.clientY;
$(document).mouseup(function(){removeHandlers()});
$(document).mousemove(function(){mymousemove()});
}
function mymousemove(e) {
e = e || window.event;
e.preventDefault();
deltaX = initialX - e.clientX;
deltaY = initialY - e.clientY;
initialX = e.clientX;
initialY = e.clientY;
$(selectedElement).css("top",(selectedElement.offset().top - deltaY) + "px");
$(selectedElement).css("left" ,(selectedElement.offset().left - deltaX) + "px");
}
function removeHandlers() {
$(document).unbind("mouseup");
$(document).unbind("mousemove");
}
function drag(element) {
var deltaX = 0, deltaY = 0, intitalX = 0, intitalY = 0;
$(element).mousedown(function()
{
selectedElement = $(element);
dragMouseDown()
} );
}