For fun I have been trying to create my own dragable divs and I have run into an issue or two.
The first and most annoying one is trying to allow the user to drag from where ever in the div they happen to click. This works great on both top left and bottom right. However on bottom left and top right they flip flop, as in you click one and it jumps to the other. I cannot seem to come up with a solution for this.
function DragMe(e) {
var relativeXPosition = (e.pageX - this.offsetLeft);
var relativeYPosition = (e.pageY - this.offsetTop);
$(document).on('mousemove',function(e) {
$('.move').offset({
top: e.pageY - relativeXPosition,
left: e.pageX - relativeYPosition,
});
});
};
$('.move').on('mousedown', DragMe);
$('.move').mouseup(function() {
$(this).off('mousedown');
$(document).off('mousemove');
$('.move').on('mousedown', DragMe);
});
This is what I have so far, also if anyone knows how I might get started on making this work on multiple divs that would be awesome. I tried before but it just snapped all the divs together and so one would disappear.
I understand this is probably do-able with jQuery UI but I was hoping to avoid that for now just to make it a bit more challenging since I'm still learning.