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

No e.clientX or e.clientY on drag event in Firefox(Firefox 中的拖动事件没有 e.clientX 或 e.clientY)
本文介绍了Firefox 中的拖动事件没有 e.clientX 或 e.clientY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经使用 Angular 中的指令实现了一个简单的拖放系统.它在 Chrome 中运行良好,但 Firefox 不会在拖动事件上公开 event.clientXevent.clientY 属性(他们只是拒绝修复它).

所以我正在寻找一个很好的替代方法来在拖动事件中公开这些属性:x,y 坐标是拖动事件的视觉反馈所必需的.

代码在(在 Chrome 和 FF 中测试).

对js的改动:

.directive('mpDrag', function($timeout, $window, $document) {//保持坐标私有和//在指令的所有实例之间共享变量鼠标X,鼠标Y;$document.on("dragover", function(event){mouseX = event.originalEvent.clientX;mouseY = event.originalEvent.clientY;})返回 {...链接:函数($范围,元素,属性){...$超时(函数(){....on('拖动', function(e) {//这里直接使用mouseX,mouseY//(顺便说一句.你应该以不同的方式检测何时隐藏元素)console.log(mouseX, mouseY);如果(e.originalEvent.clientX){el.css({'顶部':鼠标Y,左":鼠标X});} 别的 {el.css('显示', '无');}});});}};})

I've implemented a simple drag and drop system using directives in 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).

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.

Code is here - check out in Chrome and Firefox to see the problem.

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

the problem is here (beginning line 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)?

解决方案

You can hook up to dragover on document -- clientX and clientY are exposed there. Use functional closure to not populating global scope. Here is updated PLNKR (tested in Chrome and FF).

Changes to 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');
              }
            });
        });
      }
    };
  })

这篇关于Firefox 中的拖动事件没有 e.clientX 或 e.clientY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)