I have a small issue trying to make a centered div draggable. The div is centered by using absolute position and negative margin (a workaround found maybe right here). The issue is that these top and left negative margins, change the limit of the draggable area. So if I set containment: "window", the result is that the div can go outside the window (to the left and to the top) till the end of these negative margin.
I tried to synthesized this issue with this code:
$(document).ready(function(){
$("#box").draggable({containment: "window", scroll: false});
})
#box{
position:absolute;
top:50%;
left:50%;
margin-top:-50px;
margin-left: -50px;
width:100px;
height:100px;
background-color:green;
text-align:center;
line-height:8;
font-size:12px;
color:white;
}
#box:hover{
cursor:move;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</head>
<body>
<div id="box">
<span>DRAG ME A LOT</span>
</div>
</body>
</html>
Is there a way to prevent the box from going out to the left and to the top?
Many Thanks