Tôi cũng cần làm điều tương tự và tôi không muốn sử dụng tiện ích mở rộng giao diện từ eyecon.ro. Sau một số nghiên cứu, tôi tìm thấy Combining Selectables And Draggables Using jQuery UI. Nó là độc đáo nói nhưng để làm cho đoạn mã chạy bạn phải đào sâu vào nó. Tôi đã có thể làm cho nó hoạt động. Tôi hơi thay đổi nó, đây là cách của tôi để làm cho nó được thực hiện. Nó cần sửa đổi để sử dụng trên mức sản xuất, nhưng tôi hy vọng nó sẽ giúp.
// this creates the selected variable
// we are going to store the selected objects in here
var selected = $([]), offset = {top:0, left:0};
// initiate the selectable id to be recognized by UI
$("#selectable").selectable({
filter: 'div',
});
// declare draggable UI and what we are going to be doing on start
$("#selectable div").draggable({
start: function(ev, ui) {
selected = $(".ui-selected").each(function() {
var el = $(this);
el.data("offset", el.offset());
});
if(!$(this).hasClass("ui-selected")) $(this).addClass("ui-selected");
offset = $(this).offset();
},
drag: function(ev, ui) {
var dt = ui.position.top - offset.top, dl = ui.position.left - offset.left;
// take all the elements that are selected expect $("this"), which is the element being dragged and loop through each.
selected.not(this).each(function() {
// create the variable for we don't need to keep calling $("this")
// el = current element we are on
// off = what position was this element at when it was selected, before drag
var el = $(this), off = el.data("offset");
el.css({top: off.top + dt, left: off.left + dl});
});
}
});
CSS Styles để có thể xem những gì đang xảy ra:
#selectable { width: 100%; height: 100%;}
#selectable div {
background: #ffc;
line-height: 25px;
height: 25px;
width: 200px;
border: 1px solid #fcc;
}
#selectable div.ui-selected {
background: #fcaf3e;
}
#selectable div.ui-selecting {
background: #8ae234;
}
HTML Markup:
<div id="selectable">
<div>item 1</div>
<div>item 2</div>
<div>item 3</div>
<div>item 4</div>
</div>
Mã trong http://stackoverflow.com/questions/34698117/elements-became-randomly-non-resizable-after-dragging queston cũng chứa câu trả lời cho câu hỏi này Tuy nhiên mã trong câu trả lời gây mất ngẫu nhiên có thể thay đổi kích thước khi kéo – Andrus