HTML5拖动和复制



我已经看到了一些用于HTML5拖放的工作代码,但是有人有拖动和复制的示例吗?我想将元素拖到滴元素上,但仅将元素复制到此位置。

我将坚持此处显示的示例:http://www.w3schools.com/html/html5_draganddrop.asp.asp

假设我们有此文档:

<!DOCTYPE HTML>
<html>
 <head>
  <script>
    <!-- script comes in the text below -->
  </script>
 </head>
 <body>
  <div id="div1" ondrop="drop(event)"
  ondragover="allowDrop(event)"></div>
  <img id="drag1" src="img_logo.gif" draggable="true"
  ondragstart="drag(event)" width="336" height="69">
 </body>
</html>

正常拖动&amp;drop

正常的拖放功能分配给各个元素:

function allowDrop(ev) {
  /* The default handling is to not allow dropping elements. */
  /* Here we allow it by preventing the default behaviour. */
  ev.preventDefault();
}
function drag(ev) {
  /* Here is specified what should be dragged. */
  /* This data will be dropped at the place where the mouse button is released */
  /* Here, we want to drag the element itself, so we set it's ID. */
  ev.dataTransfer.setData("text/html", ev.target.id);
}
function drop(ev) {
  /* The default handling is not to process a drop action and hand it to the next 
     higher html element in your DOM. */
  /* Here, we prevent the default behaviour in order to process the event within 
     this handler and to stop further propagation of the event. */
  ev.preventDefault();
  /* In the drag event, we set the *variable* (it is not a variable name but a 
     format, please check the reference!) "text/html", now we read it out */
  var data=ev.dataTransfer.getData("text/html");
  /* As we put the ID of the source element into this variable, we can now use 
     this ID to manipulate the dragged element as we wish. */
  /* Let's just move it through the DOM and append it here */
  ev.target.appendChild(document.getElementById(data));
}

drag&amp;复制

虽然您必须更改Drop功能,以使其复制DOM元素而不是移动它。

/* other functions stay the same */
function drop(ev) {
  ev.preventDefault();
  var data=ev.dataTransfer.getData("text/html");
  /* If you use DOM manipulation functions, their default behaviour it not to 
     copy but to alter and move elements. By appending a ".cloneNode(true)", 
     you will not move the original element, but create a copy. */
  var nodeCopy = document.getElementById(data).cloneNode(true);
  nodeCopy.id = "newId"; /* We cannot use the same ID */
  ev.target.appendChild(nodeCopy);
}

尝试此页面:http://www.w3schools.com/html/tryit.asp?filename = tryhtml5_draganddrop
然后将.cloneNode(true)附加到getElementById(data)

在复制和amp之间切换糊

您甚至可以在文件管理器中进行类似:Ctrl-key开关从移动到复制:

function drop(ev) {
  ev.preventDefault();
  var data=ev.dataTransfer.getData("text/html");
  /* Within a Mouse event you can even check the status of some Keyboard-Buttons
     such as CTRL, ALT, SHIFT. */
  if (ev.ctrlKey)
  {
    var nodeCopy = document.getElementById(data).cloneNode(true);
    nodeCopy.id = "newId"; /* We cannot use the same ID */
    ev.target.appendChild(nodeCopy);
  }
  else
    ev.target.appendChild(document.getElementById(data));
}

如果您想要使用jQuery的示例,则我提供了此jsfiddle。本质上,您只需要绑定到DOM对象的dropdragoverdropstart事件。然后,您可以在clone()方法中使用jQuery的构建来复制元素。

jQuery还返回其自己的活动包装器,因此您必须从jQuery事件中获得originalevent

$('#x').bind('dragstart', function(e) {
    e.originalEvent.dataTransfer.effectAllowed = 'copy';
    e.originalEvent.dataTransfer.setData('Text', '#x');
});
$('#drop-box').bind('drop', function(e) {
    e.preventDefault();
    e.stopPropagation();
    $(this).html($(e.originalEvent.dataTransfer.getData('Text')).clone());
    return false;
}).bind('dragover', false);

最新更新