Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5: is there a way to prevent children interfering with drag events

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)

like image 353
Marius Avatar asked Sep 20 '12 09:09

Marius


People also ask

Does HTML5 support drag-and-drop?

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.

Which HTML5 attribute needs to be set in order to implement drag-and-drop feature?

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.

What is the name of the event in HTML5 when we drag an item into a valid drop point?

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.

How do you use draggable?

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.


1 Answers

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.

like image 163
Simon Robb Avatar answered Oct 19 '22 18:10

Simon Robb