【问题标题】:jQuery UI Draggable: Drag bigger div than containerjQuery UI Draggable:拖动比容器更大的 div
【发布时间】:2014-09-04 22:06:29
【问题描述】:

当我遇到问题时,我正在使用 jQuery UI Draggable 元素。我希望能够拖动比容器大的 div 元素。所以我尝试在容器上使用 position:relative 并在可拖动元素上使用 position:absolute 但它不起作用。基本上我想要完成的是:http://tech.pro/tutorial/790/javascript-tutorial-draggable-view-in-a-container

您可以在此处查看以下代码示例:http://bit.ly/1qhVxqJ

我的代码:

外部

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>

CSS

.draggable { 
width: 750px; 
height: 750px; 
padding: 0.5em; 
float: left; 
position: absolute;
background: #6C6;
border:2px solid #fff;
}

#containment-wrapper { 
height:500px;
width:700px;
background: #000;
margin-top:250px;
margin-left:500px;
border:2px solid #ccc; 
position:relative;
overflow:hidden;
}

HTML

<div id="containment-wrapper">
  <div id="draggable3" class="draggable ui-widget-content">
    <p>I'm contained within the box</p>
  </div>
</div>

jQuery

$(function() {
    $( "#draggable3" ).draggable({ cursor: "move", containment: "#containment-wrapper" });
  });

【问题讨论】:

    标签: javascript jquery html css jquery-ui


    【解决方案1】:

    您需要做的是在可拖动元素上放置一个负边距,等于它可以拖动到容器外的像素距离。无论您的可拖动对象是相对位置还是绝对位置,这都应该有效。

    可能,您希望负边距等于两个容器大小之间的差异,以便可拖动始终覆盖父级。 (这就是javascript拖动/裁剪工具通常的工作方式。)所以在你的情况下:

    .draggable { 
      margin: -250px -50px;
      top: 250px;
      left: 50px;
    }
    

    【讨论】:

    • 感谢您的快速回复!我试过了,但现在可拖动对象(文本)的起始位置在容器之外。我希望它能够定位,这样你就可以看到像(0,0)这样的文本。我更新了链接,以便您了解我的意思!
    • 好吧,你为什么不把初始css设置为top和left来补偿呢?我会更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多