I had nightmares fixing this problem for Firefox. I needed to drag the div into the diary and determine the coordinates of the fall, so that I know what date and time the user chose.
To get the drag event, I added the bottom line to the dragstart event handler:
event.dataTransfer.setData('Text', this.id);
However, the hardest part was figuring out how to get the x and y coordinates at the end of the drag, since they did not return to the dragend event handler in Firefox. I tried using mouse events, as mentioned above, but I found that they did not work while dragging and dropping, and only got called after the dragend event handler was called. So I used the dragend event to detect when the user issued the div, and then used the following mouse movement to get the coordinates and do any other work that is required. I found this to work in IE, Firefox and Chrome. Here is the html / javascript demo:
<div> <div id = "todrag" class = "testdiv" draggable="true"><p>Please drag me</p></div> <div id = "destination" class = "testdiv"><p>To here</p></div> <p id = "coords"></p> <p id = "compareords"></p> </div> <script> var down = true; var m_xcoordDrag = 0; var m_ycoordDrag = 0; var m_xcoordMove = 0; var m_ycoordMove = 0; var m_dragReleased = false; var m_coordselement = document.getElementById("coords"); var m_compareordselement = document.getElementById("compareords"); function OnMouseMove(e) { m_xcoordMove = ex; m_ycoordMove = ey; m_coordselement.innerHTML = ex + "," + ey; if (m_dragReleased) { m_compareordselement.innerHTML = "X:" + m_xcoordDrag + ", " + m_xcoordMove + " Y:" + m_ycoordDrag + ", " + m_ycoordMove; m_dragReleased = false; } } dragstart = function (event) { event.dataTransfer.setData('Text', this.id); stop = false; } dragend = function (event) { m_dragReleased = true; m_xcoordDrag = event.x; m_ycoordDrag = event.y; } document.onmousemove = OnMouseMove; var toDrag = document.getElementById("todrag"); toDrag.addEventListener('dragstart', dragstart); toDrag.addEventListener('dragend', dragend); </script>
Hope this helps!
David Christopher Reynolds
source share