2 * EasyDrag 1.5 - Drag & Drop jQuery Plug-in
4 * Thanks for the community that is helping the improvement
5 * of this little piece of code.
7 * For usage instructions please visit http://fromvega.com/scripts
12 // to track if the mouse button is pressed
13 var isMouseDown = false;
15 // to track the current element being dragged
16 var currentElement = null;
19 var dropCallbacks = {};
20 var dragCallbacks = {};
25 // global position records
31 // track element dragStatus
34 // if user is holding any handle or not
35 var holdingHandler = false;
37 // returns the mouse (cursor) current position
38 $.getMousePosition = function(e){
42 if (!e) var e = window.event;
44 if (e.pageX || e.pageY) {
48 else if (e.clientX || e.clientY) {
49 posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
50 posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
53 return { 'x': posx, 'y': posy };
56 // updates the position of the current element being dragged
57 $.updatePosition = function(e) {
58 var pos = $.getMousePosition(e);
60 var spanX = (pos.x - lastMouseX);
61 var spanY = (pos.y - lastMouseY);
63 $(currentElement).css("top", (lastElemTop + spanY));
64 $(currentElement).css("left", (lastElemLeft + spanX));
67 // when the mouse is moved while the mouse button is pressed
68 $(document).mousemove(function(e){
69 if(isMouseDown && dragStatus[currentElement.id] != 'false'){
70 // update the position and call the registered function
72 if(dragCallbacks[currentElement.id] != undefined){
73 dragCallbacks[currentElement.id](e, currentElement);
80 // when the mouse button is released
81 $(document).mouseup(function(e){
82 if(isMouseDown && dragStatus[currentElement.id] != 'false'){
84 if(dropCallbacks[currentElement.id] != undefined){
85 dropCallbacks[currentElement.id](e, currentElement);
92 // register the function to be called while an element is being dragged
93 $.fn.ondrag = function(callback){
94 return this.each(function(){
95 dragCallbacks[this.id] = callback;
99 // register the function to be called when an element is dropped
100 $.fn.ondrop = function(callback){
101 return this.each(function(){
102 dropCallbacks[this.id] = callback;
106 // disable the dragging feature for the element
107 $.fn.dragOff = function(){
108 return this.each(function(){
109 dragStatus[this.id] = 'off';
113 // enable the dragging feature for the element
114 $.fn.dragOn = function(){
115 return this.each(function(){
116 dragStatus[this.id] = 'on';
120 // set a child element as a handler
121 $.fn.setHandler = function(handlerId){
122 return this.each(function(){
123 var draggable = this;
125 // enable event bubbling so the user can reach the handle
126 bubblings[this.id] = true;
128 // reset cursor style
129 $(draggable).css("cursor", "");
131 // set current drag status
132 dragStatus[draggable.id] = "handler";
134 // change handle cursor type
135 $("#"+handlerId).css("cursor", "move");
137 // bind event handler
138 $("#"+handlerId).mousedown(function(e){
139 holdingHandler = true;
140 $(draggable).trigger('mousedown', e);
143 // bind event handler
144 $("#"+handlerId).mouseup(function(e){
145 holdingHandler = false;
150 // set an element as draggable - allowBubbling enables/disables event bubbling
151 $.fn.easydrag = function(allowBubbling){
153 return this.each(function(){
155 // if no id is defined assign a unique one
156 if(undefined == this.id || !this.id.length) this.id = "easydrag"+(new Date().getTime());
158 // save event bubbling status
159 bubblings[this.id] = allowBubbling ? true : false;
162 dragStatus[this.id] = "on";
164 // change the mouse pointer
165 $(this).css("cursor", "move");
167 // when an element receives a mouse press
168 $(this).mousedown(function(e){
170 // just when "on" or "handler"
171 if((dragStatus[this.id] == "off") || (dragStatus[this.id] == "handler" && !holdingHandler))
172 return bubblings[this.id];
174 // set it as absolute positioned
175 $(this).css("position", "absolute");
178 $(this).css("z-index", parseInt( new Date().getTime()/1000 ));
180 // update track variables
182 currentElement = this;
184 // retrieve positioning properties
185 var pos = $.getMousePosition(e);
189 lastElemTop = this.offsetTop;
190 lastElemLeft = this.offsetLeft;
194 return bubblings[this.id];