【发布时间】:2012-11-09 02:15:17
【问题描述】:
我对 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 脚本。
【问题讨论】:
-
你的 JS/JQuery 源码在哪里?您不能只发布静态 HTML 并询问 JS 为何损坏。任何控制台输出等?
-
如果您要求的教程没有成功就无法执行,我认为这不是正确的地方。您必须编写使您的生活成为噩梦之类的代码。所以,如果你做了那个教程,你认为什么是错的?你在哪里出错?这些可能是合理的问题(也提供一些代码)
-
我不想要教程。我已经用过它(HTML5ROCKS)。我想知道是否有人可以帮助我。
标签: javascript jquery html web