I’m trying to make a div draggable without using jQuery UI.
However, I’m stuck with the code below. I understand that I should use the mouse position relative to the container div (in which the div will be dragged) and that I should set the div‘s offset relative to those values.
I just can’t figure out how. Any clues?
This is the code that doesn’t (of course) work:
var X, Y;
$(this).mousedown(function() {
$(this).offset({
left: X,
top: Y
});
});
$("#containerDiv").mousemove(function(event) {
X = event.pageX;
Y = event.pageY;
});
Here’s a really simple example that might get you started:
Example: http://jsfiddle.net/Jge9z/
Not so sure about that. It seems to me that in drag and drop you’d always want to use the offset of the elements relative to the document.
If you mean you want to constrain the dragging to a particular area, that’s a more complicated issue (but still doable).