简体   繁体   English

Firefox 中的拖动事件没有 e.clientX 或 e.clientY

[英]No e.clientX or e.clientY on drag event in Firefox

I've implemented a simple drag and drop system using directives in Angular.我已经使用 Angular 中的指令实现了一个简单的拖放系统。 It works fine in Chrome, but Firefox doesn't expose event.clientX , event.clientY properties on drag event (They just refuse to fix it).它在 Chrome 中运行良好,但Firefox不会在拖动事件上公开event.clientXevent.clientY属性(他们只是拒绝修复它)。

So I'm looking for a good alternative to expose these properties on drag event: the x,y coordinates are needed for visual feedback on drag event.因此,我正在寻找一种很好的替代方法来在拖动事件上公开这些属性:拖动事件的视觉反馈需要 x,y 坐标。

Code is here - check out in Chrome and Firefox to see the problem.代码在这里- 在 Chrome 和Firefox中查看问题。

In Chrome, drag an item in the folders, you'll have the same item displayed as visual feedback following the mouse, not in Firefox (because Firefox doesn't support e.clientX and e.clientY in the drag event).在 Chrome 中,在文件夹中拖动一个项目,您将在鼠标后显示相同的项目作为视觉反馈,而不是在 Firefox 中(因为Firefox不支持拖动事件中的e.clientXe.clientY )。

the problem is here (beginning line 45):问题就在这里(从第 45 行开始):

.on('drag', function(e) {
    if (e.originalEvent.clientX) {
        el.css({
           'top': e.originalEvent.clientY + 10,
           'left': e.originalEvent.clientX + 10
        });
    } else {
        el.css('display', 'none');
    }
});

So how can I get the mouse position on screen during a drag event, in Firefox (the angular way, I mean with directives, no global variable, or whatever)?那么如何在拖动事件期间在 Firefox 中获得鼠标在屏幕上的位置(角度方式,我的意思是使用指令,没有全局变量,或者其他什么)?

在此处输入图片说明

You can hook up to dragover on document -- clientX and clientY are exposed there.您可以挂接到dragoverdocument - clientXclientY暴露在那里。 Use functional closure to not populating global scope.使用函数闭包来不填充全局范围。 Here is updated PLNKR (tested in Chrome and FF).这是更新的PLNKR (在 Chrome 和 FF 中测试)。

Changes to js:对 js 的更改:

.directive('mpDrag', function($timeout, $window, $document) {

    // keeping coordinates private and 
    // shared among all instances of the directive
    var mouseX, mouseY;

    $document.on("dragover", function(event){
      mouseX = event.originalEvent.clientX;
      mouseY = event.originalEvent.clientY;
    })

    return {
      ...
      link: function($scope, element, attrs) {
        ...
        $timeout(function() {

        ...
            .on('drag', function(e) {
              // just use mouseX, mouseY directely here
              // (btw. you should detect differently when to hide the element)
              console.log(mouseX, mouseY); 
              if (e.originalEvent.clientX) {
                el.css({
                  'top': mouseY,
                  'left': mouseX
                });
              } else {
                el.css('display', 'none');
              }
            });
        });
      }
    };
  })

You must borrow drag coordinates from the document itself:您必须从文档本身借用拖动坐标:

var dragX = 0,
    dragY = 0;

element.on('dragstart', function(e) {
    document.ondragover = function(event) {
        event = event || window.event;
        dragX = event.pageX,
        dragY = event.pageY;
    };
});

element.on('drag', function(e) {
    el.css({
        'top': dragY + 10,
        'left': dragX + 10
    });
});

Updated plunker更新的plunker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM