2

我对 HTML5 拖放 API 有一点问题。让我们编写代码,以便每个人都能理解。

<div id="draggerContainer" droppable="true">
  <div class="draggableItem" draggable="true" data-id="001">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="002">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="003">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="004">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="005">[... Stuff inside like <img> and other <div> ...] </div>
  <div class="draggableItem" draggable="true" data-id="006">[... Stuff inside like <img> and other <div> ...] </div>
</div>

好吧,这很尴尬,但我不知道如何使用拖放 api。我已经尝试过 html5rocks.com 上的教程,但没有。有人可以告诉我如何在 html5 和 Js 中启动和创建排序系统(如果可能的话,更好的 jQuery)?

这是教程的源代码。

function handleDragStart(e) {
  // Target (this) element is the source node.
  this.style.opacity = '0.4';
  dragSrcEl = this;
  e.dataTransfer.effectAllowed = 'move';
  e.dataTransfer.setData('text/html', this.innerHTML);
}
function handleDragOver(e) {
  if (e.preventDefault) {
    e.preventDefault(); // Necessary. Allows us to drop.
  }
  e.dataTransfer.dropEffect = 'move';  // See the section on the DataTransfer object.
  return false;
}
function handleDragEnter(e) {
  // this / e.target is the current hover target.
  this.classList.add('over');
}
function handleDragLeave(e) {
  this.classList.remove('over');  // this / e.target is previous target element.
}
function handleDrop(e) {
  // this / e.target is current target element.
  if (e.stopPropagation) {
    e.stopPropagation(); // stops the browser from redirecting.
  }
  // See the section on the DataTransfer object.
  return false;
}
function handleDragEnd(e) {
  // this/e.target is the source node.
  [].forEach.call(cols, function (col) {
    col.classList.remove('over');
  });
}
var cols = document.querySelectorAll('#draggerContainer .draggableItem');
[].forEach.call(cols, function(col) {
  col.addEventListener('dragstart', handleDragStart, false);
  col.addEventListener('dragenter', handleDragEnter, false)
  col.addEventListener('dragover', handleDragOver, false);
  col.addEventListener('dragleave', handleDragLeave, false);
  col.addEventListener('drop', handleDrop, false);
  col.addEventListener('dragend', handleDragEnd, false);
});

我使用了HTML5Rocks教程中使用的 Javascript 脚本。

4

2 回答 2

5

http://jsfiddle.net/WxTqd/

首先,当拖动开始时,将data-id属性作为拖动信息发送,以便您知道正在拖动哪个元素:

$("#draggerContainer")
    .on("dragstart", ".draggableItem", function(e) {
        e.dataTransfer.setData("id", $(this).data("id"));
    })

您必须继续preventDefault()进行dragover拖动工作:

.on("dragover", ".draggableItem", function(e) {
    e.preventDefault();
})

然后drop,您必须交换元素:

.on("drop", ".draggableItem", function(e) {
    // get the element being dragged according to drag information
    var id = e.dataTransfer.getData("id");

    var $draggingElem = $(".draggableItem").filter(function() {
        return $(this).data("id") === id;
    });


    // This is to make sure the element appears under the cursor
    var indexDrag = $draggingElem.index();
    var indexThis = $(this).index();

    if(indexDrag < indexThis) {
        $draggingElem.insertAfter(this);
    } else if(indexDrag > indexThis) {
        $draggingElem.insertBefore(this);
    }
});

这使用e.dataTransferjQuery 默认不公开的。要启用此功能,请在每次页面加载时执行一次:

$.event.props.push("dataTransfer");
于 2012-11-08T18:13:43.907 回答
0

查看jquery ui 可排序

这是一个非常简单的库

于 2012-11-08T18:06:36.687 回答