Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are mousemove events disabled while dragging an element?

I'm trying to move an element by watching mousemove events on the document while the element is being dragged (using html5 drag and drop). I added a mousemove listener on the document on a parent element that fires whenever I move my mouse, but once I start dragging another child element I stop seeing mousemove events and once I stop dragging I see the events again. I don't see it anywhere in the API (https://developer.mozilla.org/en-US/docs/Web/Events/mousemove) that dragging disables these events, but I can't tell how I could be stopping them from my code. Is this just part of html5 drag and drop that it disables the mousemove events while dragging?

I am using angular2 to detect mousemove. I've tried two different ways:

1)

@HostListener('document:mousemove', ['$event'])
    onMouseMove(event) {
        console.log('Global mousemove: ', event);
    }

2)

constructor(
      public element: ElementRef,
      private renderer: Renderer2) {
        element.nativeElement.draggable = true;
      }
this.mouseMoveListener = this.renderer.listen('document', 'mousemove', this.onMouseMove.bind(this));
like image 479
Alex Egli Avatar asked Sep 12 '17 22:09

Alex Egli


3 Answers

From here: "The mousemove event is fired when a pointing device (usually a mouse) is moved while over an element." When you're dragging the element mouse is not moving over it - instead it moves synchronously with the element. I think you should consider "ondrag" event for this purpose - see here.

like image 69
Alexander Leonov Avatar answered Oct 08 '22 16:10

Alexander Leonov


You can use a pointermove event, that does the job ;)

like image 40
Quentin D Avatar answered Oct 08 '22 17:10

Quentin D


You're right, mousemove events are disabled while an element is being dragged. You need to listen to a drag event instead (or perhaps to both mousemove and drag, depending on what you need). The drag event works even with a global handler.

document.addEventListener('drag', (e) => console.log(e.clientX, e.clientY))

You can test this with a dummy draggable element:

<div draggable>Test</div>
like image 1
blade Avatar answered Oct 08 '22 17:10

blade