JQueryUI - detect if item is being dragged

I want to get the position of an element when it is wrapped.

My code is:

$(document).ready(function(){ $("p").text($("div").position().left); $("p").text($("div").position().top); $("div").draggable(); }) 

This is only the position when the page loads. I want every time a div is dragged, I can write its position in the p tag.

+4
source share
2 answers
 $('#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

+8
source

You should look at the events section of jQuery's official documentation: http://jqueryui.com/draggable/#events

The drag event will be raised during drag and drop, and from there you can get the offset. For instance:

 $(this).offset().left; $(this).offset().top; 
+1
source

Source: https://habr.com/ru/post/1498432/


All Articles