forked from heyman/jquery-draggable-touch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.draggableTouch.js
115 lines (105 loc) · 3.94 KB
/
jquery.draggableTouch.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
/**
* jQuery Draggable Touch v0.5
* Jonatan Heyman | http://heyman.info
*
* Make HTML elements draggable by using uses touch events.
* The plugin also has a fallback that uses mouse events,
* in case the device doesn't support touch events.
*
* Licenced under THE BEER-WARE LICENSE (Revision 42):
* Jonatan Heyman (http://heyman.info) wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we meet some day, and
* you think this stuff is worth it, you can buy me a beer in return.
*/
;(function($){
$.fn.draggableTouch = function(action) {
// check if the device has touch support, and if not, fallback to use mouse
// draggableMouse which uses mouse events
if (!("ontouchstart" in document.documentElement)) {
return this.draggableMouse(action);
}
// check if we shall make it not draggable
if (action == "disable") {
this.unbind("touchstart");
this.unbind("touchmove");
this.unbind("touchend");
this.unbind("touchcancel");
return this;
}
this.each(function() {
var element = $(this);
var offset = null;
var end = function(e) {
e.preventDefault();
var orig = e.originalEvent;
element.trigger("dragend", {
top: orig.changedTouches[0].pageY - offset.y,
left: orig.changedTouches[0].pageX - offset.x
});
};
element.bind("touchstart", function(e) {
var orig = e.originalEvent;
var pos = $(this).position();
offset = {
x: orig.changedTouches[0].pageX - pos.left,
y: orig.changedTouches[0].pageY - pos.top
};
element.trigger("dragstart", pos);
});
element.bind("touchmove", function(e) {
e.preventDefault();
var orig = e.originalEvent;
// do now allow two touch points to drag the same element
if (orig.targetTouches.length > 1)
return;
$(this).css({
top: orig.changedTouches[0].pageY - offset.y,
left: orig.changedTouches[0].pageX - offset.x
});
});
element.bind("touchend", end);
element.bind("touchcancel", end);
});
return this;
};
/**
* Draggable fallback for when touch is not available
*/
$.fn.draggableMouse = function (action) {
// check if we shall make it not draggable
if (action == "disable") {
this.unbind("mousedown");
this.unbind("mouseup");
return this;
}
this.each(function() {
var element = $(this);
var offset = null;
var move = function(e) {
element.css({
top: e.pageY - offset.y,
left: e.pageX - offset.x,
});
};
var up = function(e) {
element.unbind("mouseup", up);
$(document).unbind("mousemove", move);
element.trigger("dragend", {
top: e.pageY - offset.y,
left: e.pageX - offset.x
});
};
element.bind("mousedown", function(e) {
var pos = element.position();
offset = {
x: e.pageX - pos.left,
y: e.pageY - pos.top
};
$(document).bind("mousemove", move);
element.bind("mouseup", up);
element.trigger("dragstart", pos);
});
});
return this;
};
})(jQuery);