Jquery drag and drop

$ (document) .ready (function () {

$("#dvv").mousedown(function() { $("#dvv").mousemove(function(e) { $("#dvv").css({ 'margin-top': e.pageY - 15, 'margin-left': e.pageX - 15 }); }); }); <div id="dvv" style="background-color: Blue; width: 150px; height: 150px; margin: 250px 550px; cursor: move;"> test Div </div> 

Div Click the button without dragging help me

+4
source share
2 answers

Please note: I have never tried to implement this before, but this seems like a start.

Note that I removed the fields from your div and added absolute positioning, so this is not the original element you started from.

Hope this gives you something to work with.

UPDATE: Just a little changed. Now, using the offset (coordinates) instead of css, it works regardless of the field settings, and absolute positioning does not need to be set.

 <div id="dvv" style="background-color: Blue; width: 150px; height: 150px; margin: 0; cursor: move;"> test Div </div> $("#dvv").mousemove(function(e) { if($(this).hasClass('moving')) { $(this).offset({ 'top': (e.pageY - $(this).data('offsety')), 'left': (e.pageX - $(this).data('offsetx')) }); } }); $("#dvv").mousedown(function(event) { $(this).data('offsetx', (event.pageX - $(this).offset().left) ); $(this).data('offsety', (event.pageY - $(this).offset().top) ); $(this).addClass('moving') }); $("#dvv").mouseup(function() { $(this).removeClass('moving') }); 
+3
source

Check out jQueryUI. It has draggable "and" droppable "which will help you with a lot of examples.

+1
source

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


All Articles