-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinydrag.js
37 lines (29 loc) · 871 Bytes
/
tinydrag.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
/*!
tinyDrag v0.9.3
(c) 2010 Ben Kay <http://bunnyfire.co.uk>
MIT license
*/
jQuery.fn.tinyDrag = function(callback) {
return jQuery.tinyDrag(this, callback);
}
jQuery.tinyDrag = function(el, callback) {
var mouseStart, elStart, moved, doc = jQuery(document), toint = parseInt, abs = Math.abs, f = false;
el.mousedown(function(e) {
moved = f;
mouseStart = {x: e.pageX, y: e.pageY};
elStart = {x: toint(el.css("left")), y: toint(el.css("top"))}
doc.mousemove(drag).mouseup(stop);
return f;
});
function drag(e) {
var x = e.pageX, y = e.pageY;
if (moved || (moved = abs(x - mouseStart.x) + abs(y - mouseStart.y) > 1))
el.css({left: elStart.x + (x - mouseStart.x), top: elStart.y + (y - mouseStart.y)});
return f;
}
function stop() {
doc.unbind("mousemove", drag).unbind("mouseup");
moved&&callback&&callback();
}
return el;
}