【问题标题】:jqueryUI resizable and draggable doesn't work togetherjqueryUI 可调整大小和可拖动不能一起工作
【发布时间】:2017-12-04 08:03:23
【问题描述】:

当我同时使用jquery-ui resizabledraggable 时遇到问题。下面提到的是问题发生时的场景:

先移动父级Div,
然后调整子 Div 的大小。
最后子 Div 显示不正确

如果我删除了可调整大小的containment 选项,就可以了。

我发现选项 containment andhelper` 不能同时存在。

谢谢

这里是演示

// external js: draggabilly.pkgd.js

$('.father').draggable({
  handle:'.p1'
}).resizable({
      helper: "ui-resizable-helper",
      containment: "parent" 
    });
$('.child').draggable({
  handle:'.p2'
}).resizable({
      helper: "ui-resizable-helper",
      containment: "parent" 
    });
body { font-family: sans-serif; }

.draggable {
  width: 300px;
  height: 300px;
  background: #F90;
  border-radius: 10px;
}
.draggable.is-pointer-down {
  background: #09F;
}
p{ background: #F00;}
.child {
  width:100px;
  height:100px;
}

.draggable.is-dragging { opacity: 0.7; }
.ui-resizable-helper{ border: 1px dotted #20a0ff; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<link href="https://jqueryui.com/resources/demos/style.css" rel="stylesheet"/>

<div class="draggable ui-widget-content father ">
  <p class ="p1">handle</p>
  <div id="resizable" class="draggable ui-widget-content child">
    <p class ="p2">handle</p>
    1234345 6
  </div>
  
</div>

【问题讨论】:

  • 我使用其他可拖动库,jqUI resizable 也无法正常工作,所以我认为 jqUI resizable 有一个错误。你能帮帮我吗?

标签: javascript jquery jquery-ui jquery-ui-draggable jquery-ui-resizable


【解决方案1】:

我会远离使用助手,我发现使用它们有很多问题。

当使用 jquery-ui 交互时,我在容器中使用了一个内部元素,这样小部件就不会受到容器中任何其他元素的影响。例如,由于“p1”元素,您的调整大小包含无法正常工作,为避免这种情况,您可以通过添加内部元素来执行我所做的事情,或者制作任何其他内部元素 position: absolute; 以便它们不会影响ui 元素。

这是我的例子:

$('.father').draggable({
  handle: '.p1'
}).resizable({});

var containTo = $('.father-inner');
$('.child').draggable({
  handle: '.p2',
  containment: containTo
}).resizable({
  containment: containTo
});
body {
  font-family: sans-serif;
}

.father-inner {
  position: absolute;
  top: 40px;
  left: 5px;
  right: 5px;
  bottom: 5px;
}

.draggable {
  width: 300px;
  height: 300px;
  background: #eee;
  border: 1px solid #ccc;
  border-radius: 10px;
}

.draggable.is-pointer-down {
  background: #09F;
}

p {
  background: #F00;
}

.child {
  min-width: 100px;
  min-height: 100px;
  height: 100px;
  width: 100px;
}

.draggable.is-dragging {
  opacity: 0.7;
}

.ui-resizable-helper {
  border: 1px dotted #20a0ff;
}

.ui-resizable-se {
  width: 20px;
  height: 20px;
  position: absolute;
  bottom: 0;
  right: 0;
  cursor: se-resize;
  background: #aaa;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div class="draggable ui-widget-content father ">
  <p class="p1">handle</p>
  <div class="father-inner">
    <div id="resizable" class="draggable ui-widget-content child">
      <p class="p2">handle</p>
      1234345 6
    </div>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多