我正在使用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" });
});