我有一个标准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重新计算位置? - >我已经尝试过启用/禁用..
答案 0 :(得分:1)
我必须做类似于你正在做的事情,这是基于可拖动区域大于容器(如可拖动的地图)创建约束。我编辑了jquery-ui.js以执行以下操作,类似于包含,但称为约束。
将其设置在您的代码中,就像收容:
$("#draggable").draggable({
axis: 'y',
constraint: 'parent'
});
第731行:
constraint: false,
第849行:
//Set a constraint if given in the options
if(o.constraint)
this._setConstraint();
第1071行:
_setConstraint: function() {
var o = this.options;
if(o.constraint == 'parent') o.constraint = this.helper[0].parentNode;
if(o.constraint == 'document' || o.constraint == 'window') this.constraint = [
0 - this.offset.relative.left - this.offset.parent.left,
0 - this.offset.relative.top - this.offset.parent.top,
$(o.constraint == 'document' ? document : window).width() - this.helperProportions.width - this.margins.left,
($(o.constraint == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
];
if(!(/^(document|window|parent)$/).test(o.constraint) && o.constraint.constructor != Array) {
var ce = $(o.constraint)[0]; if(!ce) return;
var co = $(o.constraint).offset();
var over = ($(ce).css("overflow") != 'hidden');
this.constraint = [
co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0) - this.margins.left,
co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0) - this.margins.top,
co.left+(over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderLeftWidth"),10) || 0) - (parseInt($(ce).css("paddingRight"),10) || 0) - this.helperProportions.width - this.margins.left,
co.top+(over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderTopWidth"),10) || 0) - (parseInt($(ce).css("paddingBottom"),10) || 0) - this.helperProportions.height - this.margins.top
];
} else if(o.constraint.constructor == Array) {
this.constraint = o.constraint;
}
},
第1142行,在_generatePosition函数中:
if(this.constraint) {
if(event.pageX - this.offset.click.left > this.constraint[0]) pageX = this.constraint[0] + this.offset.click.left;
if(event.pageY - this.offset.click.top > this.constraint[1]) pageY = this.constraint[1] + this.offset.click.top;
if(event.pageX - this.offset.click.left < this.constraint[2]) pageX = this.constraint[2] + this.offset.click.left;
if(event.pageY - this.offset.click.top < this.constraint[3]) pageY = this.constraint[3] + this.offset.click.top;
}