在其父级中保留一个可拖动的 div

2022-01-18 00:00:00 position limit tags jquery html

我有一个可拖动的 div 标签,我希望它在被拖动时不会从其父标签溢出.

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.

推荐答案

$('.children').draggable({ containment: "parent" });

相关文章