This page provides examples to test those ideas.
The final result is clear - don't waste your time trying to use the HTML 5 standard drag interface - it simply doesn't work!
New Approach
(The main purpose of these pages is to document why it is not ready
to be used.
A solution that actually works
is documented here.)
Once something you try fails, re-reading the documents may help to explain why or give you new ideas of what to try. That is what happened here - After the dismal failure detailed here, re-reading mozilla's Recommended Drag Types / Dragging HTML and XML made more sense. I mistakenly thought it describes a different method to control what is displayed during the drag operation. Combined with information contained in HTML5 Drag and Drop (w3.org), I created this page to test that approach.
Hint - This was also a failure!
Additional reading uncovered a technique that actually worked - it showed the entire window being dragged, not just the title bar - but it still showed a ghost. Yet another failure.
Code for the drop target
var SavedX, SavedY ; // Set when dragging begins // These events are shared by every test window on this page document.addEventListener("dragover", function(event) { event.preventDefault(); event.dataTransfer.dropEffect = 'move'; }); document.addEventListener("dragenter", function(event) { event.preventDefault(); }); document.addEventListener("drop", function(event) { event.preventDefault(); // seems to have no effect except in Firefox // without this, Firefox tries to open a new page }); |
Simple div
setData - "text/html"
HTML content may use the text/html type. The data for this type should be the serialized HTML to drag. For instance, it would be suitable to set the data value for this type to the value of the innerHTML property of an element. |
Since changing the representation of a simple window might look the same as the default, a more complex window is used. Then, as suggested by Mozilla, I included the following.
event.dataTransfer.setData("text/html", event.target.parentNode.outerHTML); |
setDragImage
One "correct" solution is to use setDragImage.
dataTransfer.setDragImage(element, x, y) Uses the given element to update the drag feedbackThis method must run the following steps:
|
Experimentation showed that provided x/y location had to be the location of the mouse pointer with respect to the of the window you want to drag.
xOffset = event.clientX - target.parentNode.offsetLeft ;// save the relative offset yOffset = event.clientY - target.parentNode.offsetTop ; event.dataTransfer.setDragImage(event.target.parentNode, xOffset, yOffset ); |
addElement
dataTransfer.addElement(element) Adds the given element to the list of elements used to render the drag feedback. |
Note: The difference between setDragImage() and addElement() is that the latter automatically generates the image based on the current rendering of the elements added (potentially keeping it updated as the drag continues, e.g. if the elements include an actively playing video), whereas the former uses the exact specified image at the time the method is invoked. |
So I tried this
event.dataTransfer.addElement(event.target.parentNode); |
Uncaught TypeError: event.dataTransfer.addElement is not a function |
Summary
OS | Browser | Simple | setData - "text/html" | setDragImage | addElement |
---|---|---|---|---|---|
Win XP | Chrome 49 | Shows a ghost when dragging | Same as Simple | Shows window as ghost | Fails |
Firefox 24 | Fail - clientXY = 0 during ondragend | ||||
Win 10 | Chrome 55 | Shows a ghost when dragging cursor in wrong position | Same as Simple | Shows window as ghost - worthless cursor in wrong position | Fails |
Firefox 47 | Fail - clientXY = 0 during ondragend | ||||
Edge 25 | Moves - uses a copy, not a ghost | Only shows title | Does not support property | Does not support property | |
IE 11 | Shows ghost, moves fine | Does not support property | Does not support property | Does not support property |
I guess the reason is obvious - The drag interface is so poorly implemented across browsers that there is no reason to waste time documenting the differences!
Author: Robert Clemenzi