I'm having a bit of a problem with html5 drag and drop. I don't see an easy way out of it. Basically i have some "boxes" with some other html elements inside. The parent boxes are draggable and they can be dropped on each other.
I bind dragover
event on the body to handle drag-drop on the entire page. Problem is, when you drag over the boxes - the event is sometimes triggered on child elements and the parent doesn't get this event at all.
Is there an easy way to prevent this from happening?
Basically i want the dragover event to fire as soon as the mouse is in the area of the target box. I know of a couple of ways of solving this, but they're really ugly and i was wondering if there maybe is something simple.
Thanks for your thoughts
Short version of what i'm doing in code:
document.addEventListener('dragenter', function(e) {
if (e.target.className == 'candrophere')
// cancel out "e" to allow drop
}, false);
But in my case the child elements are taking up almost the entire '.candrophere' box so the event is almost never fired on the correct target (especially when i move the mouse faster)
Now HTML 5 came up with a Drag and Drop (DnD) API that brings native DnD support to the browser making it much easier to code up. HTML 5 DnD is supported by all the major browsers like Chrome, Firefox 3.5 and Safari 4 etc.
getData() method. This method will return any data that was set to the same type in the setData() method. The dragged data is the id of the dragged element ("drag1") Append the dragged element into the drop element.
The ondrop event occurs when a draggable element or text selection is dropped on a valid drop target. Drag and drop is a very common feature in HTML5. It is when you "grab" an object and drag it to a different location.
Enable draggable functionality on any DOM element. Move the draggable object by clicking on it with the mouse and dragging it anywhere within the viewport.
The css pointer-events
rule prevents mouse interaction events from firing on the elements it is applied to, but isn't currently supported by IE (http://caniuse.com/pointer-events).
A workaround I've used for this problem is to add something like
.is-drag-in-progress .child-elements
{
pointer-events:none;
}
to your CSS, and add the is-drag-in-progress
class to the body
element in your onDragStart
handler (and remove it when the drag ends). This will prevent the child elements from interfering with drag events on the parent element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With