简体   繁体   English

如何使用拖放 HTML API 显示可放置区域

[英]How to show droppable area using Drag and Drop HTML API

I'm implementing a functionality where the user can sort the list and re-order the elements, on dragover I want to show the droppable area below the dragover element.我正在实现一个功能,用户可以在其中对列表进行排序并重新排序元素,在拖动时我想显示拖动元素下方的可放置区域。

Note: I want to achieve this using native javascript注意:我想使用本机 javascript 来实现这一点

Without seeing your code I can't suggest an exact implementation but you can use JS to inject a "placeholder" element.没有看到您的代码,我无法建议一个确切的实现,但您可以使用 JS 来注入“占位符”元素。

First, you'll need to know the position of all your items, maybe use an array to log the order of your list items.首先,您需要知道所有项目的位置,也许使用数组来记录列表项目的顺序。

Next, on dragover append the placeholder element in the place of where the dragged item you are moving is located on the list.接下来,在拖动时将占位符元素附加到列表中您要移动的被拖动项目所在的位置。 For example, if you have a list like:例如,如果您有一个如下列表:

ITEM 1
ITEM 2
ITEM 3
ITEM 4

When you drag ITEM 4 over ITEM 3 then inject the placeholder after ITEM 2 so it appears between ITEM 2 and 3. When you drop ITEM 3, you can remove the placeholder.当您将 ITEM 4 拖到 ITEM 3 上,然后在 ITEM 2 之后插入占位符,使其出现在 ITEM 2 和 3 之间。当您放下 ITEM 3 时,您可以移除占位符。

Note, you'll also have to remove the injected placeholder as you move further up/down the list.请注意,当您进一步向上/向下移动列表时,您还必须删除注入的占位符。 Otherwise, you'll end up with multiple placeholders.否则,您最终会得到多个占位符。

There are also plugins available for this such as HTML5 Sortable and SortableJS也有可用的插件,例如HTML5 SortableSortableJS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM