Are mousemove events disabled while dragging an element?

你。 提交于 2020-07-08 11:28:05

问题


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));

回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/46186173/are-mousemove-events-disabled-while-dragging-an-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!