How do I get clientX and clientY to work inside my "drag" event handler on Firefox?
Asked Answered
Z

3

11

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.

Zarzuela answered 20/5, 2009 at 11:4 Comment(4)
+1 for the comment you left. You can use the answer provided by José. I'll also try to find out something related to this, as soon as I get some time.Coaming
I found a solution, I've placed a listener on the "dragover" event at the document level, now I get the right X and Y properties that I can expose through a globally shared object.Nathannathanael
The same bug (in my opinion) is present up through Firefox 10. Same goes for pageX and other coordinate properties. If it's not supposed to be part of this event it should be undefined, not zero right? This is very annoying. Sorry to hear you have to use the dragover event as a hack but at least that does workSupranational
Does this answer your question? Drag and drop directive , no e.clientX or e.clientY on drag event in FireFoxExcruciating
Z
17

I found a solution, I've placed a listener on the "dragover" event at the document level, now I get the right X and Y properties that I can expose through a globally shared object.

Zarzuela answered 23/5, 2009 at 20:0 Comment(0)
A
3

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!

Abaca answered 20/5, 2009 at 11:14 Comment(1)
onmousemove event is not firing while the mouse is downLefthanded
C
0

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.

Coaming answered 20/5, 2009 at 11:35 Comment(1)
JQuery and Prototype just provide simulated D&D which is not what I'm looking for, native DD is much more robust and fast than any other simulated solution.Nathannathanael

© 2022 - 2024 — McMap. All rights reserved.