Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep a draggable div inside its parent

I have got a draggable div tag and I'd like it not to overflow from its parent while being dragged.

<script>
    $('.children').resizeable().draggable();//from jquerUI
</script>

<style>
    .parent{
        overflow: hidden;
        position: relative;
        width: 1000px;
        height: 1000px;
    }
    .children{
        width: 100px;
        height: 100px;
        position: absolute;
    }
</style>

<div class="parent">
   <div class="children"><div>
</div>

How can I limit the children's position and size inside the parent.

like image 424
Trần Minh Avatar asked Aug 02 '12 15:08

Trần Minh


2 Answers

$('.children').draggable({ containment: "parent" });
like image 157
Eugene H Avatar answered Sep 30 '22 16:09

Eugene H


Here's a complete working demo of a draggable element inside another draggable element.

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Draggable - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <style>
  #draggable { width: 150px; height: 150px; padding: 0.5em; }
  </style>
  <script>
  $(function() {
    $( "#draggable" ).draggable();
    $( "#draggable1" ).draggable({containment: "parent"});
  });
  </script>
</head>
<body>

<div id="draggable" class="ui-widget-content draggable">
  <p>Drag me around</p>
  <div id="draggable1" class="ui-widget-content draggable">
    <p>Drag me around</p>
  </div>
</div>


</body>
</html>
like image 34
Anderson Green Avatar answered Sep 30 '22 14:09

Anderson Green