简体   繁体   English

如何使用HTML5通过拖放来移动元素?

[英]How to shift elements with Drag and Drop by using HTML5?

I'm working with Drag and Drop tutorial from HTML Rocks. 我正在使用HTML Rocks中的拖放教程 The current tutorial basically does a switch of the dragged element and the element it is dropped on. 当前教程基本上对拖动的元素和放置在其上的元素进行切换。 What was in point A goes to point B and point B goes to point A. A点的内容转到B点,B点的内容转到A点。

|A|B|C|  -->  |B|A|C|
|D|E|F|       |D|E|F|

I'm trying to change this logic. 我正在尝试更改此逻辑。 If I drag point E, it should be in between the others. 如果拖动点E,则它应该在其他点之间。

|A|B|C|  -->  |A|E|B|
|D|E|F|       |C|D|F|

This is the demo I have for this. 这是我的演示

Should this be done changing the property dropEffect or it will be necessary to create a new div for adding the item we're moving? 是否应该通过更改属性dropEffect来完成此操作,还是必须创建一个新的div来添加要移动的项目?

function handleDragStart(e) {
  this.style.opacity = '0.4';  // this / e.target is the source node.
}

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;
}

I found the way to do this thanks to @tbirell 我感谢@tbirell找到了实现此目标的方法

I'm using the Jquery UI library for this and change all the logic of what I did at the beginning 我为此使用了Jquery UI ,并更改了我一开始所做的所有逻辑

Instead of using DIV, I'm replacing them by UL and LI 我没有使用DIV,而是用UL和LI代替了它们

<ul id="sortable">
  <li class=" ui-state-default">Item 1</li>
  <li class=" ui-state-default">Item 2</li>
  <li class=" ui-state-default">Item 3</li>
  <li class=" ui-state-default">Item 4</li>
  <li class=" ui-state-default">Item 5</li>
  <li class=" ui-state-default">Item 6</li>
</ul>

The library is the one that manages the logic of this. 该库是管理此逻辑的库。

$( function() {
    $( "#sortable" ).sortable({
      revert: true
    });
    $( "#draggable" ).draggable({
      connectToSortable: "#sortable",
      helper: "clone",
      revert: "invalid"
    });
    $( "ul, li" ).disableSelection();
  } );

A link of the working demo . 工作演示的链接。

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

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