Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While drag over the absolute element, drag leave event has been trigger continuously

We are using the position absolute element as a child of position relative element in our application. We have used the javascript drag and drop events for our custom functions.

Issue :

While drag the files over the absolute element it fluctuated.

Sample: https://stackblitz.com/edit/typescript-avv5u1?file=index.ts

Steps to reproduce:

1.Drag any files in to drop target.

2.While you are hovering over the target, absolute element will be displays with yellow background.

3.Hover the dragged file over the yellow region. Now yellow, region will fluctuate.

Can you please suggest how can i resolve this issue in our side?

like image 732
Karthik Ravichandran Avatar asked Mar 11 '19 04:03

Karthik Ravichandran


People also ask

What is drag leave?

The ::drag-leave signal is emitted on the drop site when the cursor leaves the widget. A typical reason to connect to this signal is to undo things done in GtkWidget::drag-motion , e.g. undo highlighting with gtk_drag_unhighlight().

Which attribute defines the drag start?

What is being dragged is defined by the document or application where the drag was started. img elements and a elements with an href attribute have their draggable attribute set to true by default.


2 Answers

Consider debouncing and throttling your event handler. Adding basic implementation.

  let droptarget = document.getElementById('droptarget');
    droptarget.addEventListener('dragover', function(e: any) {
        droptarget.classList.add('drag-hover');
        e.preventDefault();
        e.stopPropagation();
    })

    var eventThrottled = false;

    function dragHandler() {
      if(eventThrottled) {
          return;
        }
      droptarget.classList.remove('drag-hover');
      eventThrottled = true;
      setTimeout(()=>{eventThrottled = false;},2000);
    }

    droptarget.addEventListener('dragleave', dragHandler);

This disables the handler for 2000ms.

like image 94
YetAnotherBot Avatar answered Oct 11 '22 14:10

YetAnotherBot


CSS Solution

Disable pointer-events for all children of the drop target parent.

#droptarget * {
  pointer-events: none;
}

enter image description here

CodeSandbox

like image 24
Andy Hoffman Avatar answered Oct 11 '22 14:10

Andy Hoffman