jquery - 当到达容器时调整元素的大小
我有一个标准 div,设置为拖动和拖动。在其父级内调整大小。我需要能够在将 div 拖入其父级时调整其大小。我只需要在将其垂直拖动到底部时发生这种情况,因此我将其设置如下:
$("#draggable").draggable({
drag: function(event, ui) { AtBottom(); },
axis:'y', containment:'parent'
});
然后我计算可拖动元素及其容器的底部位置(减去偏移量),并在它更大时重新调整其大小..
function AtBottom(){
var Cpos = $('#Container').position();
var cheight = $('#Container').height();
var Boundry = Cpos.top+cheight;
var pos = $("#draggable").position();
var height = $("#draggable").height();
var bottom = pos.top+height+10; /*10 as offset */
if(bottom>Boundry){
$("#draggable").height((height-10));
}
}
看来 jquery 在整个拖动过程中存储元素属性并且不会重新计算它们,这对于性能来说是有意义的,但不允许它按照我想要的方式工作 ->否则它就会按其应有的方式进行。每次拖动时,高度都会减少 10。
有没有办法可以强制 Jquery 重新计算位置? ->我已经尝试过启用/禁用..
I have a standard div that is set to both drag & resize within it's parent. I need to be able to resize the div as it is dragged into it's parent. I only need this to happen when it's dragged vertically to the bottom, so I have it setup like this:
$("#draggable").draggable({
drag: function(event, ui) { AtBottom(); },
axis:'y', containment:'parent'
});
Then I am calculating the bottom position of both the draggable element and it's container(minus offset), and re-sizing it when it's greater..
function AtBottom(){
var Cpos = $('#Container').position();
var cheight = $('#Container').height();
var Boundry = Cpos.top+cheight;
var pos = $("#draggable").position();
var height = $("#draggable").height();
var bottom = pos.top+height+10; /*10 as offset */
if(bottom>Boundry){
$("#draggable").height((height-10));
}
}
It appears that jquery stores the element properties throughout the entire drag and doesn't re-calculate them, which makes sense for performance, but isn't allowing this to work the way I would like -> Otherwise it does as it should. On each drag, it decreases the height by 10.
Is there a way I can force Jquery to re-calculate the positions? -> I already tried enable/disable..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须做一些类似于您正在做的事情,即根据可拖动区域大于容器(如可拖动地图)创建一个约束。我编辑了 jquery-ui.js 来执行以下操作,类似于包含,但称为约束。
像遏制一样在代码中设置它:
第 731 行:
第 849 行:
第 1071 行:
第 1142 行,在 _generatePosition 函数中:
I had to do something similar to what it sounds like you're doing, which is create a constraint based on the draggable area being bigger than the container (like a draggable map). I edited jquery-ui.js to do the following, similar to the containment, but called constraint.
Set it in your code like you would containment:
line 731:
line 849:
line 1071:
line 1142, in the _generatePosition function: