我有两个要素:

1. 固定高度的 parent overflow:hidden
2. 它的子级,具有较大的固定高度。

<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>

我希望能够在父div内上下拖动列表。我正在使用jQuery ui draggable插件来实现verticle拖动,但是我不确定如何将列表限制在父div内。
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"></script>
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.draggable.js"></script>

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

如何设置列表的顶点的上下限?

最佳答案

使用containment option:

$('.myList').draggable({
    axis: 'y',
    containment: 'parent'
});

关于javascript - 设置可拖动对象的边界限制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1547662/

10-09 17:45
查看更多