2013-07-02 117 views
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> 

回答

1

可以刪除此行:

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

利用該線釋放時,元件向上跳躍(表現爲一個靜態元素);沒有它,它被放置在它被釋放的地方 - 「鼠標指向的地方」。

+0

它的工作原理。非常感謝! – yiro

+0

不客氣。隨意打勾:) –

+0

嗨,Anday,我創建了一個頁面的方法。但拖動的元素總是落在一個陌生的地方,這不是鼠標指向的地方。這裏是代碼:[鏈接](http://jsfiddle.net/yiro/GvMPw/)你能看看嗎?拖動的元素在放下後不能再次拖動。我也對這個問題感到困惑。非常感謝。 – yiro