jquery ui drag/drop getting position from multiple draggables

北城余情 提交于 2019-12-25 03:22:36

问题


I have a droppable container with >1 draggables in it. I want to simply be able to see the position of each draggable every time any one of them is dragged. Sounds easy, but I'm having a tough time figuring it out. Here's what I have:

<script type="text/javascript">
$(function() {
    $("#draggable").draggable({ containment: '#droppable' });
    $("#draggable2").draggable({ containment: '#droppable' });
    $("#droppable").droppable({
        drop: function(event, ui) {
            document.getElementById('position1').value = ui.position.top + ', ' + ui.position.left; 
        }
    });

});
</script>

Basically, where I have the position1 input id gathering the position, I want to have a second line do the same thing for the other draggable.


回答1:


In your case, you don't need to use the ui parameter. Since you know what elements are draggable, you can select them specifically and find their positions. Also, since you're using jQuery, do yourself a favor and replace document.getElementById with $. :-)



来源:https://stackoverflow.com/questions/1646492/jquery-ui-drag-drop-getting-position-from-multiple-draggables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!