2011-06-14 22 views

回答

2

問題是,jQuery UI在拖動時應用position:absolute - 導致元素位於最靠近定位元素的top:0left:0或窗口。

解決此問題的一個選項是使用定製助手來使用helper option進行拖動。您可以使用jQuery UI的position()實用方法的相對助手輕鬆定位到原始的元素如下:

$('#container').sortable({ 
 
    axis: 'y', 
 
    helper: function(event, elm) { 
 
    return $(elm).clone().position({ 
 
     my: "left", 
 
     at: "left", 
 
     of: elm 
 
    }); 
 
    } 
 
});
* { /*for stack snippet demo*/ 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
#container { 
 
    width: 200px; 
 
    text-align: center; 
 
    height: 300px; 
 
    border: 1px solid green; 
 
    position: relative; 
 
} 
 
.draggable { 
 
    width: 100px; 
 
    height: 100px; 
 
    background: yellow; 
 
    margin: 10px auto; 
 
    cursor: move; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> 
 
<div id="container"> 
 
    <div class="draggable"></div> 
 
    <div class="draggable"></div> 
 
</div>