yiro
yiro

Reputation: 1

drag and drop put the dropped element at mouse position

I want to do drag-and-drop with jquery. The following is the testing code. But what I want is to drop the #draggable at the place where my mouse points to, instead of just appending #draggable to a definite place in #droppable. I appreciate it very much if anyone can help.

<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Droppable - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>#draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
#droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
</style>
<script>
$(function() {
var dropHelp=true
$( "#draggable" ).draggable({
  revert:'invalid',
  helper:'clone',
});
$( "#droppable" ).droppable({
    //   accept:"#draggable",
    //   activeClass: "ui-state-hover",
    //   hoverClass: "ui-state-active",
       drop: function( event, ui ) {
       if(dropHelp){
       //clone and remove positioning from the helper element
           var newDiv = $(ui.helper).clone(true)
           .removeClass('ui-draggable-dragging')
           .css({position:'relative', left:0, top:0});     

       $(this).append(newDiv);

    //drop the draggable source element
    } else {
       $(this).append(ui.draggable);
    }
  }
});
});
</script>
</head>
<body>

<div id="draggable" class="ui-widget-content">
<p>Drag me to my target</p>
</div>

<div id="droppable" class="ui-widget-header">
<p>Drop here</p>
</div>
</body>
</html>

Upvotes: 0

Views: 3503

Answers (1)

Andy G
Andy G

Reputation: 19367

You can remove this line:

//.css({position:'relative', left:0, top:0});

With this line the element jumps upwards when released (behaving as a static element); without it, it is positioned where it is released -"where the mouse points".

Upvotes: 1

Related Questions