简体   繁体   English

从另一个div内部拖放一个div

[英]Drag and Drop a div from the inside of another div

I'm trying the drag and drop function of jQuery UI but how can I drag a div1 inside a div2 and drop in div3 like this. 我正在尝试jQuery UI拖放功能,但是如何将div1拖放到div2并像这样放入div3 这个

My currrent code is like this 我的当前代码是这样的

css: CSS:

#div2 {
     height: 52em !important;
     min-height: 50em;
     overflow:hidden;
}

script: 脚本:

$( function() {
    $('#div1').draggable({
        containment: '#div4'
    });

    $('#div3').droppable();
});

My code doesn't seem able to drag the div1 to div3 我的代码似乎无法将div1拖动到div3

If you're wanting the actual DOM element to be moved into the new one you can do something like this (I'm not sure if there's a super fancy jQuery UI way): 如果您希望将实际的DOM元素移动到新元素中,则可以执行以下操作(我不确定是否有一种超赞的jQuery UI方法):

$( function() {
    $('#div1').draggable({
        containment: '#div4'
    });

    $('#div3').droppable({ drop: function(e, opts) { 
        $(this).append(opts.draggable.detach());
    }});
});

Basically this will detach the draggable element from the DOM on the drop and append it to the droppable element. 基本上,这将从拖放上的DOM中分离出draggable元素,并将其附加到droppable元素上。

You'll want to remove the positioning (relative and top/left) from the element after you drop it otherwise it will be positioned weird. 放置元素后,您需要从元素中删除其位置(相对位置和顶部/左侧),否则该元素的位置将很奇怪。

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

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