views:

497

answers:

2

I have two elements:

1) a parent of fixed height, overflow:hidden

2) its child, of larger fixed height.

<style type="text/css">
    .myList {list-style:none; margin:0px; padding:1px;}
    .myList li{ height:50px;  margin:4px; padding:2px;}
    .dragPanel {height:230px; border:solid; overflow:hidden;}
</style>

<div class="dragPanel">
 <ul class="myList">
  <li>1</li>
  <li>2</li>
  ...   ....
  <li>8</li>
  <li>9</li>
 </ul>
</div>

I want to be able to drag the list up and down within the parent div. I am using the jquery ui draggable plugin to achieve verticle dragging, but I am unsure how to constrain the list within the parent div.

<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.draggable.js"&gt;&lt;/script&gt;

<script type="text/javascript">
    $(document).ready(function() {
        $('.dragPanel').addClass("hover");
        $('.myList').draggable({ axis: 'y' });
    });
</script>

How would I go about setting verticle upper and lower limits for the posistion of the list?

Thanks

A: 

Use the containment option:

$('.myList').draggable({
    axis: 'y',
    containment: 'parent'
});
moff
The parent is of smaller hieght than its child - when I try this option the dragging doesn't work - the list just sort of jumps between the top and bottom positions
Paul
A: 

Ok,

Here is the route I have gone down...

When the page is loaded, I add a container div around the draggable list. I set it's height to twice that of the list, then position it up the page a little:

<script type="text/javascript">
  $(document).ready(function() {
      CreateContainerDiv();
      $('.dragPanel').addClass("hover");
      $('.myList').draggable({ axis: 'y', containment: 'parent' });
  });

    function CreateContainerDiv() {
        var dragPanel = $('.dragPanel');
        var dragTarget = $('.myList');

        // add the container panel
        var newPanelHeight = dragTarget.outerHeight() * 2 - dragPanel.outerHeight();
        dragTarget.wrap(document.createElement("div"));

        // set its height and put it in the right place
        dragTarget.parent().css("height", newPanelHeight);
        dragTarget.parent().css("position", "relative");
        var newPanelPosition = ((dragTarget.outerHeight() * -1) / 2);
        dragTarget.parent().css("top", newPanelPosition);         
    }
</script>

The list is then contained to this new element.

This appears to do the job, but the positioning is all a little shakey if the list has any padding / margin applied. Any thoughts on that would be appreicated!

------Edit ---------

Ok, I think I have solved the placement issue. I have turned this into a plugin so other people don't have to spend time creating this functionality.

A demo is available: http://webpangea.blogspot.com/2009/10/draggable-scrolling-list-with-jquery.html

And grab the code from google code: http://code.google.com/p/draggablelist/

Paul