HTML drag event does not fire in firefox

后端 未结 3 837
攒了一身酷
攒了一身酷 2021-01-17 20:22

I have a table on which I need to implement draggable header columns. I implemented it using Chrome as my browser, and everything worked fine. When I tested it in Firefox (1

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-17 20:49

    I had nightmares fixing this issue for Firefox. I needed to drag a div onto a diary and detect the coordinates of the drop so I knew which date and time the user had selected.

    To get the drag event to fire I added the below line to the dragstart event handler:

    event.dataTransfer.setData('Text', this.id);
    

    However, the hardest thing to work out was how to get the x and y coordinates when the drag ended, as these are not returned in the dragend event handler in Firefox. I tried using mouse events as mentioned above, but I found that these did not work while the drag is in progress and are only called after the dragend event handler has been called. So, I only used the dragend event to detect when the user had released the div, and then used the next mouse moved event to get the coordinates and do any other work that is required. I found this works in IE, Firefox and Chrome. Here is the html / javascript of a demo:

     

    Please drag me

    To here

    I hope this helps!

提交回复
热议问题