Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the position of a draggable object

I'm trying to get the x and y of the draggable object using jQuery.

The scenario is, I'm dragging and dropping an object onto another object and want to get the position of the drag & drop object.

Edit: Now I can get the position of the object but I need more:

Here is what I've tried:

<script> $(function() {     $('#draggable').draggable({         drag: function() {             var offset = $(this).offset();             var xPos = offset.left;             var yPos = offset.top;             $(this).text('x: ' + xPos + 'y: ' + yPos);         }     });      $("#droppable").droppable({         drop: function(event, ui) {             $(this)                 .addClass("ui-state-highlight")                 .find("p")                 .html("Dropped!");         }     }); }); </script> 

Now I can get the position of the draggable object but I need it to be resizable and in addition to getting x,y of the draggable, I also need the size of it.

like image 846
Pabuc Avatar asked Feb 04 '11 22:02

Pabuc


People also ask

How do you get the position of the draggable element?

The . position() method allows us to retrieve the current position of an element relative to the offset parent. Contrast this with . offset() , which retrieves the current position relative to the document.

What is the syntax of the draggable function?

The draggable (option) method specifies that an HTML element can be moved in the HTML page. Here, the option parameter specifies the behavior of the elements involved. Syntax: $(selector, context).

How do you make a draggable object?

To make an object draggable set draggable=true on that element. Just about anything can be drag-enabled: images, files, links, files, or any markup on your page. Our example creates an interface to rearrange columns that have been laid out with CSS Grid.

What is draggable in?

The draggable global attribute is an enumerated attribute that indicates whether the element can be dragged, either with native browser behavior or the HTML Drag and Drop API. draggable can have the following values: true : the element can be dragged.


2 Answers

You can use the drag event:

$('#dragThis').draggable({     drag: function() {         var offset = $(this).offset();         var xPos = offset.left;         var yPos = offset.top;         $('#posX').text('x: ' + xPos);         $('#posY').text('y: ' + yPos);     } }); 

JS Fiddle demo.

This demo brought to you by the drag event, and the methods offset() and text().


Edited in response to comment from OP, to original question:

David, actually what I need is a bit more complicated but you might do great help. What I want to do is to put a draggable object and a droppable image in a panel and drag the draggable object on the image and get the position of where it was dropped. I'll also need the draggable object to be resizable and get the size of it at the end too :) thank you

...uh, whoah...

I think this sort of covers the basics of what's asked for:

$('#dragThis').draggable( {     containment: $('body'),     drag: function() {         var offset = $(this).offset();         var xPos = offset.left;         var yPos = offset.top;         $('#posX').text('x: ' + xPos);         $('#posY').text('y: ' + yPos);     },     stop: function() {         var finalOffset = $(this).offset();         var finalxPos = finalOffset.left;         var finalyPos = finalOffset.top;          $('#finalX').text('Final X: ' + finalxPos);         $('#finalY').text('Final X: ' + finalyPos);     },     revert: 'invalid' });  $('#dropHere').droppable({     accept: '#dragThis',     over: function() {         $(this).animate({             'border-width': '5px',             'border-color': '#0f0'         }, 500);         $('#dragThis').draggable('option', 'containment', $(this));     } }); 

Updated JS Fiddle demo.

Newly-updated JS Fiddle demo, featuring revert: invalid, so dropping the draggable div anywhere but the droppable div will cause the draggable to animate back to its starting position. If that'd be appreciated. Or desired at all...

To address the requirement for the draggable object to be resizable as well, I don't think that this is possible, since both draggable() and resizable() respond to the same interaction. It may be possible, in some way, but it's currently beyond my ken, I'm afraid.


Edited to add in the 'height/width' requirement, and also to tidy up a few things and improve the CSS. That said:

HTML:

<div id="dragThis">     <ul>         <li id="posX">x: <span></span></li>         <li id="posY">y: <span></span></li>         <li id="finalX">Final X: <span></span></li>         <li id="finalY">Final Y: <span></span></li>         <li id="width">Width: <span></span></li>         <li id="height">Height: <span></span></li>     </ul> </div>  <div id="dropHere"></div> 

CSS:

#dragThis {     width: 8em;     height: 8em;     padding: 0.5em;     border: 3px solid #ccc;     border-radius: 0 1em 1em 1em;     background-color: #fff;     background-color: rgba(255, 255, 255, 0.5); }  #dragThis span {     float: right; }  #dragThis span:after {     content: "px"; }  li {     clear: both;     border-bottom: 1px solid #ccc;     line-height: 1.2em; }  #dropHere {     width: 12em;     height: 12em;     padding: 0.5em;     border: 3px solid #f90;     border-radius: 1em;     margin: 0 auto; } 

jQuery:

$('#dragThis').draggable({     containment: $('body'),     drag: function() {         var offset = $(this).offset();         var xPos = offset.left;         var yPos = offset.top;         $('#posX > span').text(xPos);         $('#posY > span').text(yPos);     },     stop: function() {         var finalOffset = $(this).offset();         var finalxPos = finalOffset.left;         var finalyPos = finalOffset.top;          $('#finalX > span').text(finalxPos);         $('#finalY > span').text(finalyPos);         $('#width > span').text($(this).width());         $('#height > span').text($(this).height());     },     revert: 'invalid' });  $('#dropHere').droppable({     accept: '#dragThis',     over: function() {         $(this).animate({             'border-width': '5px',             'border-color': '#0f0'         }, 500);         $('#dragThis').draggable('option', 'containment', $(this));     } }); 

JS Fiddle demo, of the above.

like image 136
David Thomas Avatar answered Sep 22 '22 04:09

David Thomas


You can try one of these:

$(this).position() 

or

$(this).offset() 

The .position() method allows us to retrieve the current position of an element relative to the offset parent. Contrast this with .offset(), which retrieves the current position relative to the document.

From http://api.jquery.com/position/

like image 37
user603881 Avatar answered Sep 24 '22 04:09

user603881