你試圖含有不是它的父元素的可調整大小(#2
)
嘗試將容器設置爲其父.TimelineContainer
:
$(".Event3").resizable({
axis: "x",
handles: "e, w",
containment: ".TimelineContainer"
})
Fiddle
更新:
從JQuery-UI文檔看來,包含選項的使用目的是在父元素的範圍內使用。按照您的意圖使用此選項似乎不可能。嘗試是這樣的:
$(".Event3").resizable({
axis: "x",
handles: "e, w",
containment: ".TimelineContainer",
resize: function(event, ui) {
// Define the dimensions of this element
var left = ui.position.left;
var width = ui.size.width;
var right = left + width;
// Define my constraints based on the element #2
var constraintLeft = $('#2').position().left;
var constraintWidth = $('#2').width();
var constraintRight = constraintLeft + constraintWidth;
// if resizable has gone further left than the constraint
if(left < constraintLeft) {
var left = parseInt($(this).css('left'));
// measure the difference between this left and the constraint left
var difference = constraintLeft - left;
// make up the difference
$(this).css('left', constraintLeft);
// make sure your width stays the same
$(this).width($(this).width() - difference);
// cancel the event
$(this).resizable('widget').trigger('mouseup');
}
// if resizable has gone further right than the constraint
if(right > constraintRight) {
// change the width to fit between the constraint right and this left
$(this).width(constraintRight - left);
// cancel the event
$(this).resizable('widget').trigger('mouseup');
}
}
});
如果您手動定義可調整大小的邊界,然後你調用一個鼠標鬆開時,可調整大小已經達到了這些邊界。下面是一起玩小提琴:
Fiddle
我想實際限制牽引到div#2 - 作爲拖動以目前的形式確實 – user2616550
對不起沒有也抑制拖動到邊界(#2)這正是我需要的 – user2616550
@ user2616550這是因爲我手動定義了resize事件中的邊界。 'constraintLeft'和'constraintRight'是根據元素#2的比例指定的。我在代碼中添加了一些註釋。 –