简体   繁体   中英

How do I get clientX and clientY to work inside my “drag” event handler on Firefox?

Mozilla Firefox 3.x seems to have a bug when listening to the "ondrag" event. The event object doesn't report the position of the object being dragged, clientX , clientY , and other screen offsets are all set to zero.

This is quite problematic as I wanted to make a proxy element based on the element being dragged and using of course, clientX and clientY to adjust its position.

I know that there's cool stuff around such as setDragImage in HTML5 but I want to provide a generic abstraction for native DD between browsers.

Faulty code:

document.addEventListener('drag', function(e) {
    console.log(e.clientX); // always Zero
}, false);

Note:

This problem doesn't happen on other events ( dragstart , dragover ) and the mousemove events cannot be captured while dragging something.

我找到了一个解决方案,我在文档级别的“dragover”事件上放置了一个侦听器,现在我获得了可以通过全局共享对象公开的正确 X 和 Y 属性。

The drag event in HTML 5 is not fully functional in todays browsers. To simulate a drag n drop situation you should use:

  1. Add a onmousedown event, setting a var true.
  2. Add a onmouseup event, setting that var false.
  3. Add a onmousemove event, checking if that var is true, and if it is, move your div according to the coordinates.

This always worked for me. If you face any problems, get in touch again, I can provide some examples.

good luck!

I know that there's cool stuff around such as setDragImage in HTML5 but I want to provide a generic abstraction for native DD between browsers.

But why do something like this, aren't there libraries like JQuery & Prototype available for cross browser drag & drop?

Or else if you want to implement a DD library of your own, you can take help of their methods or extend them, as both the libraries are following object oriented paradigm.

This will save much time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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