I want to get the position of an element when it is beeing dragged.
My code so far:
$(document).ready(function(){
$("p").text($("div").position().left);
$("p").text($("div").position().top);
$("div").draggable();
})
This only gets the position when the page loads. I want to detect whenever the div is beeing dragged so I can write its position in the p tag.
$('#dragThis').draggable(
{
drag: function(){
var offset = $(this).offset();
var xPos = offset.left;
var yPos = offset.top;
$('#posX').text('x: ' + xPos);
$('#posY').text('y: ' + yPos);
}
});
JSFIDDLE
You should have a look at the events section of the jQuery official documentation: http://jqueryui.com/draggable/#events
The drag
event will be raised while being dragged and from there you can get the offset. For example:
$(this).offset().left;
$(this).offset().top;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With