javascript - jqueryUI 可调整大小和可拖动不能一起工作

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

当我同时使用 jquery-ui ressizeddraggable 时遇到问题。以下为问题发生时的场景:

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

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

我发现选项containment 和helper`不能同时共存。

谢谢

这是演示

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

最佳答案

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

使用 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>

关于javascript - jqueryUI 可调整大小和可拖动不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47628931/

相关文章:

javascript - 类似 ios 的动画,可将内容从右向左滑动

javascript - 从子页面导航到主页面并滚动到id的方法

javascript - 在 jQueryUI slider 上的禁用和启用之间切换

javascript - 我无法从 Javascript 触发自定义事件

javascript - 找不到 npm 模块

php - 类似于 php 中的 facebook 的新闻行情

javascript - jQuery 下拉切换查找事件 li 并保持下拉列表打开

jQuery 效果、动画和 CSS

javascript - jquery Accordion 自定义点击事件

javascript - 通过 jQuery 获取没有文本的 HTML 片段