[英]How to get the position of the preview of a draggable element when it's dropped in HTML5 DnD
[英]How to get relative position of a draggable item when dropped?
我有以下几点:
$(function() {
// Make images draggable.
$(".item").draggable({
// Find position where image is dropped.
stop: function(event, ui) {
// Show dropped position.
var Stoppos = $(this).position();
$("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top);
}
});
});
当我放下它时,它将获得该项目的位置,但是它是相对于浏览器窗口的,而不是它们所在的div的位置。 我如何获得他们的相对位置?
编辑->这是html / css:
<style type="text/css">
.container {
margin-top: 50px;
cursor:move;
}
#screen {
overflow:hidden;
width:500px;
height:500px;
clear:both;
border:1px solid black;
}
</style>
<body>
<div class="container">
<div id="screen">
<img id="productid_1" src="images/KeypadLinc OFF.jpg" class="item" alt="" title="" />
<img id="productid_2" src="images/KeypadLinc ON.jpg" class="item" alt="" title="" />
</div>
</div>
<div id="stop">Waiting image getting dropped...</div>
</body>
设置包含属性。 例如:
$(".item").draggable( { containment: 'parent' } );
请记住,containment元素(在本例中为parent
)可能不适用于某些选择器。
从文档中:
限制拖动到指定元素或区域的范围内。 可能的字符串值:“父”,“文档”,“窗口”,[x1,y1,x2,y2]。
有关更多信息,请参见jQuery UI文档 。
给出父div position: relative
。
#screen {
position: relative;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.