简体   繁体   English

可拖放的 Jquery UI 开关位置

[英]Draggable and Droppable Jquery UI Switch Position

I am learning Jquery Draggable and Droppable right now.我现在正在学习 Jquery Draggable 和 Droppable。 I would like to create switchable content.我想创建可切换的内容。 I have this scheme:我有这个方案:

 <div class="droppableContainer"> <div class="draggableContent" id="Content1"> Hi I am Content 1. </div> </div> <div class="droppableContainer"> <div class="draggableContent" id="Content2"> Hi I am Content 2. </div> </div>

If, I drag my Content1 and drop it into another droppableContainer, I would like to switch it with Content2.如果,我将我的 Content1 拖放到另一个 droppableContainer 中,我想用 Content2 切换它。 So after I dropped Content1, Content2 move to the Content1 droppableContainer like shown below.所以在我删除 Content1 之后,Content2 移动到 Content1 droppableContainer,如下所示。

 <div class="droppableContainer"> <div class="draggableContent" id="Content2"> Hi I am Content 2. </div> </div> <div class="droppableContainer"> <div class="draggableContent" id="Content1"> Hi I am Content 1. </div> </div>

Any suggestion to do it ?有什么建议吗? Thanks谢谢

I solved this using sortable() .我使用sortable()解决了这个问题。

First, you need to wrap your draggable items inside a single container, like this:首先,您需要将可拖动项目包装在一个容器中,如下所示:

<div class="droppableContainer">
  <div class="draggableContent" id="Content2">
    Hi I am Content 2.
  </div>
  <div class="draggableContent" id="Content1">
    Hi I am Content 1.
  </div>
</div>

And add this js:并添加这个js:

$(".droppableContainer").sortable({
    distance: 20, //distance is optional. This indicates the number of pixels that the mouse must be moved before the sorting starts.
    placeholder: "highlight"
}); 
$( ".droppableContainer" ).disableSelection(); 

Then, optionally you can style your highlight class with some CSS if you want to make it look like this example .然后,如果你想让它看起来像这个例子,你可以选择使用一些 CSS 来设置highlight类的样式。

You can check this demo I made in Codepen您可以查看我在Codepen 中制作的这个演示

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

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