简体   繁体   English

放置时如何获取可拖动项目的相对位置?

[英]How to get relative position of a draggable item when dropped?

I have the following: 我有以下几点:

    $(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);
    }

    });
});

This will get the position of the item when I drop it, but it is relative to the browser window, not the div they are within. 当我放下它时,它将获得该项目的位置,但是它是相对于浏览器窗口的,而不是它们所在的div的位置。 How can I get their relative position? 我如何获得他们的相对位置?

edit-> Here is the html/css: 编辑->这是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>

Set the containment attribute. 设置包含属性。 For example: 例如:

$(".item").draggable( { containment: 'parent' } );

Keep in mind that the containment element (in this case parent ) may not work with certain selectors. 请记住,containment元素(在本例中为parent )可能不适用于某些选择器。

From the documentation: 从文档中:

Constrains dragging to within the bounds of the specified element or region. 限制拖动到指定元素或区域的范围内。 Possible string values: 'parent', 'document', 'window', [x1, y1, x2, y2]. 可能的字符串值:“父”,“文档”,“窗口”,[x1,y1,x2,y2]。

See the jQuery UI Documentation for more information. 有关更多信息,请参见jQuery UI文档

Give the parent div position: relative . 给出父div position: relative

#screen {
    position: relative;
    ...
}

暂无
暂无

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

相关问题 将可拖动元素拖放到HTML5 DnD中时如何获取预览的位置 - How to get the position of the preview of a draggable element when it's dropped in HTML5 DnD 可拖动的可放置-放置时保持在原位 - Draggable droppable - stay in position when dropped 获取鼠标相对于放置的div的位置 - Get mouse relative position to dropped div jQuery-获取放置元素的相对位置和属性 - jQuery - get relative position and attribute of dropped elements 如何使可拖动元素在放下时保持在新位置(HTML5 不是 jQuery)? - How to make a draggable element stay at the new position when dropped (HTML5 not jQuery)? 当将可拖动的项目放到可放置的容器上时使它们消失 - Make the draggable item fade away when dropped onto droppable contianer 当拖放到“垃圾箱”中时,jQuery可拖动项正在更改页面的href - jQuery draggable item is changing the href of the page when dropped into “trash” 当css作为绝对位置并且其父div将css作为相对位置时,如何使图像可拖动? - How to make a image draggable when it has css as position absolute and it's parent div has css as position relative? 删除后如何删除jQuery可拖动项的子项 - How to remove children of jquery draggable item after dropped 在连接到可拖动列表的jQuery可排序列表中,如何告诉可拖动项目放置到的特定列表项? - in a jQuery sortable connected to a draggable list, how can I tell the particular list item that the draggable item was dropped onto?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM