【发布时间】:2015-09-29 13:26:30
【问题描述】:
如果有人能帮我弄清楚如何使 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() 尝试了 width 和 height,但这似乎也不是正确的做法。
【问题讨论】:
-
没有详细查看 dragFix 函数(可能,你可以限制那里的值而不是使用包含),当我测试它时,边界本身似乎确实有效,但需要拖动的元素尺寸被减去:
var bounds = container.getBoundingClientRect(); var dragrect = $('.draggable')[0].getBoundingClientRect() .... containment: [bounds.x,bounds.y, bounds.right - dragrect.width, bounds.bottom - dragrect.height](小提琴:jsfiddle.net/z0gqy9w2/4) -
@Me.Name 嗯,右边和底部似乎工作,但现在顶部和左边不工作。编辑 Dragfix 可能是一个可能的解决方案。好主意。
-
糟糕,使用 x 和 y 代替左右,x 和 y 在 firefox 中工作,所以没有问题。这也适用于 Chrome(尚未测试 ie):
containment: [bounds.left,bounds.top, bounds.right - dragrect.width, bounds.bottom - dragrect.height](jsfiddle.net/z0gqy9w2/5)(不过,在 dragfix 中进行工作感觉更通用,稍后可能会看一下) -
@Me.Name 感谢您对此进行破解!如果 draggable container is inside a scrollable container.
标签: javascript jquery jquery-ui jquery-draggable jquery-droppable