[英]How to drag a div within a div?
I want to add some div
using drag and drop mechanism. 我想使用拖放机制添加一些div
。 I have a main div in which I add some other: When I drag div
texted "HI" into below div
, I append two other div
s called Drag and Drop into the main div
. 我在主div中添加了其他一些内容:当将div
文本为“ HI”的div
拖动到div
下方时,我将另外两个名为Drag和Drop的div
附加到主div
。
Now what I want is to have the ability to drag the div
texted "Drag", and drop it into div
texted "Drop". 现在,我想要的是能够拖动div
文本“ Drag”并将其拖放到div
文本“ Drop”的功能。 How can I achieve that? 我该如何实现?
Here is my code: 这是我的代码:
$(function() { $(".draggable").draggable({ revert: true, helper: 'clone', start: function(event, ui) { $(this).fadeTo('fast', 0.5); }, stop: function(event, ui) { $(this).fadeTo(0, 1); } }); $("#div2").draggable({ start: function(event, ui) { alert('div2-drag'); $(this).fadeTo('fast', 0.5); }, stop: function(event, ui) { $(this).fadeTo(0, 1); } }); $("#div1").droppable({ hoverClass: 'active', drop: function(event, ui) { alert('div1-drop'); //this.value = $(ui.draggable).text(); $("#mydiv").append('<div id="div1" ></div><div id="div2" ></div> '); } }); $("#mydiv").droppable({ hoverClass: 'active', drop: function(event, ui) { alert('mydiv - drop'); //this.value = $(ui.draggable).text(); $("#mydiv").append('<div id="div1" > Drop</div><div id="div2" ondragstart="drag(event)" > Drag</div> '); } }); $("#mydiv_second").droppable({ hoverClass: 'active', drop: function(event, ui) { alert('mydivsecond- drop'); //this.value = $(ui.draggable).text(); $("#mydiv_second").append('<div id="div1" > Drop</div><div id="div2" ondragstart="drag(event)" > Drag</div> '); } }); });
body { font-family: sans-serif; font-size: 11pt; } .draggable { width: 250px; height: 20px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: default; } .active { border: 2px solid #6699ff; } #mydiv { width: 350px; height: 300px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: default; } #mydiv_second { width: 350px; height: 300px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: default; } #div1 { width: 150px; height: 70px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: default; } #div2 { width: 30px; height: 30px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: move; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"> <div class="draggable">hi!</div> <div id="mydiv"></div> <div id="mydiv_second"></div>
The problem why you can't do drag-and-drop of the div
texted "Drag" is that you are executing .draggable()
and .dropable()
methods before the elements are created (you do it when the page loads). 为什么不能拖放.dropable()
为“ Drag”的div
的问题是在创建元素之前正在执行.draggable()
和.dropable()
方法(在页面加载时执行)。
The issue if fixed if you move this code: 如果您移动以下代码,则该问题已解决:
$("#div2").draggable({
start: function(event, ui) {
alert('div2-drag');
$(this).fadeTo('fast', 0.5);
},
stop: function(event, ui) {
$(this).fadeTo(0, 1);
}
});
$("#div1").droppable({
hoverClass: 'active',
drop: function(event, ui) {
alert('div1-drop');
//this.value = $(ui.draggable).text();
$( "#mydiv" ).append( '<div id="div1" ></div><div id="div2" ></div> ' );
}
});
And place it in the $("#mydiv").droppable
section, right after you have created both #div1
and #div2
(notice that you are always creating the div
s with the same id, and that could create problems when you add more than one). 将其放置在$("#mydiv").droppable
部分中,就在创建#div1
和#div2
(注意,您始终在创建具有相同ID的div
,并且在添加时可能会产生问题)超过一个)。
The result would be like this: 结果将是这样的:
$(function() { $(".draggable").draggable({ revert: true, helper: 'clone', start: function(event, ui) { $(this).fadeTo('fast', 0.5); }, stop: function(event, ui) { $(this).fadeTo(0, 1); } }); $("#mydiv").droppable({ hoverClass: 'active', drop: function(event, ui) { alert('mydiv - drop'); //this.value = $(ui.draggable).text(); $( "#mydiv" ).append( '<div id="div1" > Drop</div><div id="div2" ondragstart="drag(event)" > Drag</div> ' ); $("#div2").draggable({ start: function(event, ui) { alert('div2-drag'); $(this).fadeTo('fast', 0.5); }, stop: function(event, ui) { $(this).fadeTo(0, 1); } }); $("#div1").droppable({ hoverClass: 'active', drop: function(event, ui) { alert('div1-drop'); //this.value = $(ui.draggable).text(); $( "#mydiv" ).append( '<div id="div1" ></div><div id="div2" ></div> ' ); } }); } }); $("#mydiv_second").droppable({ hoverClass: 'active', drop: function(event, ui) { alert('mydivsecond- drop'); //this.value = $(ui.draggable).text(); $( "#mydiv_second" ).append( '<div id="div1" > Drop</div><div id="div2" ondragstart="drag(event)" > Drag</div> ' ); $("#div2").draggable({ start: function(event, ui) { alert('div2-drag'); $(this).fadeTo('fast', 0.5); }, stop: function(event, ui) { $(this).fadeTo(0, 1); } }); $("#div1").droppable({ hoverClass: 'active', drop: function(event, ui) { alert('div1-drop'); //this.value = $(ui.draggable).text(); $( "#mydiv" ).append( '<div id="div1" ></div><div id="div2" ></div> ' ); } }); } }); }); function drag(e) {}
body { font-family: sans-serif; font-size: 11pt; } .draggable { width: 250px; height: 20px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: default; } .active { border: 2px solid #6699ff; } #mydiv { width: 350px; height: 300px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: default; } #mydiv_second { width: 350px; height: 300px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: default; } #div1 { width: 150px; height: 70px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: default; } #div2 { width: 30px; height: 30px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: move; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"> <div class="draggable">hi!</div> <div id="mydiv"></div> <div id="mydiv_second"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.