最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I get clientX and clientY to work inside my "drag" event handler on Firefox? - Stack Overf

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jul 29, 2021 at 11:54 0Valt 10.3k9 gold badges39 silver badges64 bronze badges asked May 20, 2009 at 11:04 Christophe EbléChristophe Eblé 8,1613 gold badges35 silver badges33 bronze badges 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. – Kirtan Commented May 20, 2009 at 13:01
  • 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. – Christophe Eblé Commented May 20, 2009 at 13:23
  • 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 work – Chris Bosco Commented Mar 9, 2012 at 4:04
  • Does this answer your question? Drag and drop directive , no e.clientX or e.clientY on drag event in FireFox – 0Valt Commented Jul 29, 2021 at 11:53
Add a comment  | 

3 Answers 3

Reset to default 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.

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论