Although the question states native JavaScript, I'm going to respond given that @Moe Sweet commented that jQuery was enabled.
Drag and drop is a particular event chain. It's not obscenely difficult, but it can be challenging.
Simply put, drag and drop is the following steps/events:
- Mouse pressed down, dragging is enabled
- Mouse is moved, dragging continues
- Mouse is let up, dragging is disabled
The simple solution is to use those events: mousedown, mousemove, and mouseup
$(anElement).mousedown(foodown);
function foodown(){
$(window).mousemove(foomove).mouseup(fooup);
//stuff
}
function foomove(){
//stuff
}
function fooup(){
//stuff
$(window).unbind('mousemove', foomove).unbind('mouseup', fooup);
}
A point that should be noticed is that the events following the mousedown need to be bound to the window so that they are caught if the mouse has left the original element while being held down.
The "stuff" part of the code is where you'll need to check the screen coords of the element vs the screen coords of the mouse and move things appropriately. Motion is only really necessary during the mousemove event as the mouse wont have moved for a mouseup or mousedown event.
There is a similar question where I posted my code for a jQuery.dragondrop plugin.
jQuery.draggable? It's going to be quite complex for full browser support with no jQuery at all; if you can use straight jQ, use.mousedown()and.mouseup()and.mousemove(). Store the starte.pageXande.pageYvalues along with the starting dialog position, and use the deltas to move stuff around..draggable()to work (with jQuery, not native JS)?