拖放将放下的元素放在鼠标位置

时间:2013-07-02 22:34:40

标签: jquery drag-and-drop

我想用jquery进行拖放。以下是测试代码。但我想要的是将#draggable放在我的鼠标指向的地方,而不是仅仅将#draggable附加到#droppable中的一个确定位置。如果有人能提供帮助,我非常感激。

<!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>

1 个答案:

答案 0 :(得分:1)

您可以删除此行:

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

使用此行,元素在释放时向上跳跃(表现为静态元素);没有它,它被放置在它被释放的位置 - “鼠标指向的地方”。