HTML5排序DIV

Cla*_*tta 2 html javascript jquery html5 web

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>
Run Code Online (Sandbox Code Playgroud)

嗯,这是awkard但我不知道如何使用拖放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);
});
Run Code Online (Sandbox Code Playgroud)

我使用过HTML5Rocks教程中使用的Javascript脚本.

pim*_*vdb 6

http://jsfiddle.net/WxTqd/

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

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

你必须preventDefault()dragover得到拖工作:

.on("dragover", ".draggableItem", function(e) {
    e.preventDefault();
})
Run Code Online (Sandbox Code Playgroud)

然后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);
    }
});
Run Code Online (Sandbox Code Playgroud)

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

$.event.props.push("dataTransfer");
Run Code Online (Sandbox Code Playgroud)