I am currently struggling with HTML5 drag'n drop as I'd like to achieve something specific.
I have a div that can be dragged using the HTML5 API, but I only want the drag to occur if the user really means it (as it triggers some more handlers afterwards).
That is why I trigger a dragstart
when a certain condition is met from mousemove
.
var fnMousemove = function (oEvent) {
if (myCondition) {
document.removeEventListener("mousemove", fnMousemove);
oDraggable.setAttribute("draggable", true);
oDraggable.addEventListener("dragstart", fnDragstart);
oDraggable.addEventListener("drag", fnDrag);
oDraggable.addEventListener("dragend", fnDragend);
// Fire a dragstart event, now that we know what to do with it
var e = document.createEvent("MouseEvents"); //$NON-NLS-1$
e.initEvent("dragstart"); //$NON-NLS-1$
oDraggable.dispatchEvent(e);
}
};
But as you can already guess, that's not working in the sense that the dragstart
is properly fired, but no other drag event (drag
, dragend
...).
Is there something I'm not properly handling there ? Or can you see a workaround to launch the browser's drag'n drop from a mousemove handler ? (mouse button being held down)
Thanks in advance!
You can find a simple running example here