forked from nathco/jQuery.dragmove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjQuery.dragmove.js
executable file
·152 lines (106 loc) · 4.69 KB
/
jQuery.dragmove.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Draggable functionality for DOM elements
// Source: github.com/ByNathan/jQuery.dragmove
// Version: 1.0
(function($) {
$.fn.dragmove = function(options) {
return this.each(function() {
var hasOptions = false;
if (typeof options !== 'undefined') {
hasOptions = true;
}
var $document = $(document),
$this = $(this),
active,
startX,
startY;
var isChromeMobile = (navigator.userAgent.toLowerCase().indexOf('chrome') > -1 && navigator.userAgent.toLowerCase().indexOf('android') > -1);
$this.on('mousedown touchstart', function(e) {
active = true;
if (isChromeMobile) {
startX = e.originalEvent.touches[0].pageX - $this.offset().left;
startY = e.originalEvent.touches[0].pageY - $this.offset().top;
} else {
startX = e.originalEvent.pageX - $this.offset().left;
startY = e.originalEvent.pageY - $this.offset().top;
}
if ('mousedown' == e.type)
click = $this;
if ('touchstart' == e.type)
touch = $this;
if (window.mozInnerScreenX == null)
return false;
});
$document.on('mousemove touchmove', function(e) {
if (isChromeMobile && active) {
e.preventDefault();
}
if (active) {
var newPos = getNewPosition(e);
}
if ('mousemove' == e.type && active)
click.offset({
left: newPos.left,
top: newPos.top
});
if ('touchmove' == e.type && active)
touch.offset({
left: newPos.left,
top: newPos.top
});
}).on('mouseup touchend', function() {
active = false;
});
function getNewPosition (e) {
var newTop;
var newLeft;
var obj;
var pageX;
var pageY;
if (isChromeMobile) {
pageX = e.originalEvent.touches[0].pageX;
pageY = e.originalEvent.touches[0].pageY;
} else {
pageX = e.originalEvent.pageX;
pageY = e.originalEvent.pageY;
}
if ('mousemove' == e.type && active) {
obj = click;
}
if ('touchmove' == e.type && active) {
obj = touch;
}
if (hasOptions && typeof options.regionDiv !== 'undefined') {
var maxLeft = options.regionDiv.offset().left;
var maxTop = options.regionDiv.offset().top;
var maxRight = options.regionDiv.offset().left + options.regionDiv.outerWidth();
var maxBottom = options.regionDiv.offset().top + options.regionDiv.outerHeight();
var clickRight = (pageX - startX) + obj.width();
var clickBottom = (pageY - startY) + obj.height();
if (pageX - startX > maxLeft) {
newLeft = pageX - startX;
} else {
newLeft = maxLeft;
}
if (pageY - startY > maxTop) {
newTop = pageY - startY;
} else {
newTop = maxTop;
}
if (clickRight > maxRight) {
newLeft = maxRight - obj.width();
}
if (clickBottom > maxBottom) {
newTop = maxBottom - obj.height();
}
} else {
newLeft = pageX - startX;
newTop = pageY - startY;
}
return {
left: newLeft,
top: newTop
};
}
});
};
})(jQuery);