如果有人能帮我解决如何使可拖动元素被包含在一个根据窗口大小改变比例的 div 中,我将非常感激您的指导。
如果我这样做:
当我使用
我也尝试过使用
我已经尝试过从
如果我这样做:
element.draggable({
cursor: "move",
containment: '#container'
});
当我使用
transform: scale(1.5)
时,它会按照容器的普通大小来控制拖动元素的范围。也就是说,会有部分空间不能被可拖动元素所占用。我也尝试过使用
containment: 'parent'
,但它会出现很多问题。
编辑
我已经找到如何获取顶部和左侧的限制,但我无法弄清楚如何获取右侧和底部的限制。var containmentArea = $("#container");
containment: [containmentArea.offset().left, containmentArea.offset().top, ???, ???]
我已经尝试过从
containmentArea[0].getBoundingClientRect()
获取宽度和高度,但这似乎也不是正确的方法。
var bounds = container.getBoundingClientRect(); var dragrect = $('.draggable')[0].getBoundingClientRect() .... containment: [bounds.x,bounds.y, bounds.right - dragrect.width, bounds.bottom - dragrect.height]
(示例:http://jsfiddle.net/z0gqy9w2/4/) - Me.Namecontainment: [bounds.left,bounds.top, bounds.right - dragrect.width, bounds.bottom - dragrect.height]
(http://jsfiddle.net/z0gqy9w2/5/)(不过,在dragfix中完成工作会更通用,稍后可能会研究一下)。 - Me.Name