拖动元素时是否禁用mousemove事件? [英] Are mousemove events disabled while dragging an element?

查看:303
本文介绍了拖动元素时是否禁用mousemove事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过拖动元素时(使用html5拖放)观看文档上的mousemove事件来移动元素.我在文档上的父元素上添加了mousemove侦听器,每当我移动鼠标时,该元素就会触发,但是一旦我开始拖动另一个子元素,我便停止看到mousemove事件,而一旦我停止拖动,便再次看到该事件.我在API的任何位置都看不到它( https://developer. mozilla.org/zh-CN/docs/Web/Events/mousemove ),这些操作会禁用这些事件,但是我无法告诉我如何从代码中阻止它们.这只是html5拖放的一部分,它会在拖动时禁用mousemove事件吗?

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?

我正在使用angular2来检测mousemove.我尝试了两种不同的方式:

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

推荐答案

来自 -相反,它会与元素同步移动.我认为您应该为此目的考虑"ondrag"事件-请参阅此处.

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.

这篇关于拖动元素时是否禁用mousemove事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆