jquery - 当到达容器时调整元素的大小

发布于 2024-08-04 07:38:52 字数 841 浏览 2 评论 0原文

我有一个标准 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2024-08-11 07:38:52

我必须做一些类似于您正在做的事情,即根据可拖动区域大于容器(如可拖动地图)创建一个约束。我编辑了 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;
  }

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;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文