views:

724

answers:

1

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..

A: 

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:

$("#draggable").draggable({
    axis: 'y',
    constraint: 'parent'
});

line 731:

constraint: false,

line 849:

//Set a constraint if given in the options
if(o.constraint)
  this._setConstraint();

line 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;
    }

  },

line 1142, in the _generatePosition function:

  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;
  }
JJfitzgerald