gpt4 book ai didi

javascript - HTML5 排序 DIV

转载 作者:太空宇宙 更新时间:2023-11-04 14:42:31 24 4
gpt4 key购买 nike

我在使用 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);
});

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

最佳答案

http://jsfiddle.net/WxTqd/

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

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

您必须在 dragover 上使用 preventDefault() 才能使拖动正常工作:

.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.dataTransfer,默认情况下 jQuery 不公开它。要启用此功能,请在每次页面加载时执行一次:

$.event.props.push("dataTransfer");

关于javascript - HTML5 排序 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13295013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com