我有一个标准的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重新计算位置?->我已经试过启用/禁用了..
发布于 2010-09-29 05:27:37
我不得不做一些听起来像你正在做的事情,那就是基于可拖动区域比容器大的情况下创建一个约束(就像一个可拖动的地图)。我编辑了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;
}https://stackoverflow.com/questions/1392456
复制相似问题