我認爲這很簡單,但在可調整大小的容器中放置可拖動元素時,似乎在jQuery UI中存在一些錯誤。在可調整大小的容器中使用可拖動元素的遏制問題
由於調整大小手柄(jsfiddle here)創建的額外高度,下面的代碼允許將可拖動元素稍微放在容器的外部。
HTML
<div class="container2">
<div class="item2"></div>
</div>
CSS
.container2 {
min-width:300px;
min-height:200px;
outline:1px solid black;
}
.item2 {
width:50px;
height:50px;
background-color:red;
}
jQuery的
$(".container2").resizable();
$(".item2").draggable({
containment: ".container2"
}).resizable({
containment: ".container2"
});
所以我周圍這讓通過設置圍堵與使用容器的座標following code:
$(".item2").draggable({
start: function (event, ui) {
var containmentX1 = $(".container2").offset().left;
var containmentY1 = $(".container2").offset().top;
var containmentX2 = ($(".container2").outerWidth() + $(".container2").offset().left - $(this).outerWidth())
var containmentY2 = ($(".container2").outerHeight() + $(".container2").offset().top - $(this).outerHeight())
$(this).draggable({
containment: [containmentX1, containmentY1, containmentX2, containmentY2]
});
},
}).resizable({
containment: ".container2"
});
這工作正常,除了一個問題...包容座標只設置第二次拖動元素被移動。
我用創建而不是開始繞過這個,但如果容器被調整大小,座標不會改變。
是否有辦法在首次拖動時將座標識別爲包容區域?