0

我想用 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>
4

1 回答 1

1

您可以删除此行:

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

使用这条线,元素在释放时向上跳跃(表现为静态元素);没有它,它就会被定位在它被释放的地方——“鼠标指向的地方”。

于 2013-07-02T22:48:03.610 回答