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

javascript - event.dataTransfer.dropEffect in chrome - Stack Overflow

programmeradmin0浏览0评论

I have a script which determines the result of drag and drop operation from the value of event.dataTransfer.dropEffect. I set the property event.dataTransfer.effectAllowed to value "copymove". In Firefox the value of dropEffect is "move" by default and "copy" when I press ctrl key during drag. In Chrome, the value of dropEffect is always "none". Does Chrome not suppot this API?

I have a script which determines the result of drag and drop operation from the value of event.dataTransfer.dropEffect. I set the property event.dataTransfer.effectAllowed to value "copymove". In Firefox the value of dropEffect is "move" by default and "copy" when I press ctrl key during drag. In Chrome, the value of dropEffect is always "none". Does Chrome not suppot this API?

Share Improve this question asked Sep 25, 2013 at 16:22 jlx84jlx84 1011 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

This is a bug in Chrome (and in Internet Explorer). For Chrome there is a bug report: https://bugs.chromium/p/chromium/issues/detail?id=39399

As a solution you must store the content of dropEffect in a global variable while the drag events and use its content instead of event.dataTransfer.dropEffect if that value is "none" in the drop event.

I got a solution for this as it is still not working these years.

Only Firefox seems to update the value of event.dataTransfer.dropEffect automatically.

In all other browsers you will have to do some extra steps (provided in script below) by using keyboard events (event.ctrlKey and event.shiftKey) to capture the status of CTRL and SHIFT keys and update the event.dataTransfer.dropEffect.

Solution

The basic script I attach here, updates the event.dataTransfer.dropEffect and makes things work in Chrome and in Edge as well.

document.addEventListener("dragover", (event) => {
  event.preventDefault();
  var c = event.ctrlKey;
  var s = event.shiftKey;
  var dropEffect = updateDropEffect(c, s);  
  event.dataTransfer.dropEffect = dropEffect;

  document.getElementById("div1").innerHTML = "drag type: " + dropEffect;;

    }, false);


function updateDropEffect(c, s) {
    // control, shift, none behaviors
    if (c && s) { return "link"; }
    if (c) { return "copy" }
    return "move";
  }
<div id="drag1"draggable="true">DRAG THIS TEXT BUT KEEP MOUSE BUTTON PRESSED WHILE PRESSING CTRL + SHIFT KEY 
  <br> You will notice that mouse pointer icon changes, not only in Firefox but also in in Chrome and Edge
  <br><br>
</div>
<div id="div1">*** Look here for status of mouse pointer icon *** </div>    

发布评论

评论列表(0)

  1. 暂无评论