Javascript 如何在外部div中包含此可调整大小的div?

Javascript 如何在外部div中包含此可调整大小的div?,javascript,jquery,Javascript,Jquery,我有一个可调整大小和拖拽的盒子(灰色)。可以通过从角拉伸长方体来调整其大小 我想确保在任何时候,内部的灰色框都不能被拖出外部的黄色框。内部灰色框的边缘应最大限度地接触外部黄色框的边缘 我的密码在这里。 HTML: <div id="outer" style="background-color: yellow; width: 600px; height: 400px; margin-left: 40px; margin-top: 50px;"> <div

我有一个可调整大小和拖拽的盒子(灰色)。可以通过从角拉伸长方体来调整其大小

我想确保在任何时候,内部的灰色框都不能被拖出外部的黄色框。内部灰色框的边缘应最大限度地接触外部黄色框的边缘

我的密码在这里。

HTML:

  <div id="outer" style="background-color: yellow; width: 600px; height: 400px; margin-left: 40px; margin-top: 50px;">


        <div class="draggable_div rot">
            <div class="rotatable">
                <div id="inner" class="resizable" style="background-color: #3C3C3C; width: 300px; height: 300px;">
                </div>
            </div>
        </div>

  </div>

  <div id="x_and_y_of_inner">Left: , Right: </div>
    $('.draggable_div').draggable();


    $( ".draggable_div" ).draggable({
      drag: function( event, ui ) {

        var left_most_cordinates = $("#outer").offset().left;
        var top_most_cordinates = $("#outer").offset().top;

        $("#x_and_y_of_inner").text( "Left: " + left_most_cordinates + " Top: " + top_most_cordinates );

        ui.position.left = Math.min( left_most_cordinates, ui.position.left );
        ui.position.top = Math.min( top_most_cordinates, ui.position.top );
      }
    });

    $('.resizable').resizable({
        aspectRatio: true,
        handles: 'ne, se, sw, nw'
    });



    $(document).on('mouseover', '.rot', function(){
        var tc = $(this);
        tc.rotatable({
            handle:tc.children('.rotate.left, .rotate.right')
        });
        return true;
    });
.draggable_div
{
    position: relative; 
    display: -moz-inline-stack;
    display: inline-block;
    vertical-align: top;
    zoom: 1;
    *display: inline;   

    cursor: hand; 
    cursor: pointer;       
}

.resizable
{
    width: 50%;   
    border: 1px solid #bb0000;   
}
.resizable img
{
    width: 100%;   
}

.ui-resizable-handle 
{
    background: #f5dc58;
    border: 1px solid #FFF;
    width: 9px;
    height: 9px;

    z-index: 2;
}
.ui-resizable-se
{
    right: -5px;
    bottom: -5px;
}

.ui-rotatable-handle 
{
    background: #f5dc58;
    border: 1px solid #FFF;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -o-border-radius: 5px;
    -webkit-border-radius: 5px;
    cursor: pointer;

    height:        10px;
    left:          50%;
    margin:        0 0 0 -5px;
    position:      absolute;
    top:           -5px;
    width:         10px;
}
.ui-rotatable-handle.ui-draggable-dragging
{
    visibility:  hidden;
}
CSS:

  <div id="outer" style="background-color: yellow; width: 600px; height: 400px; margin-left: 40px; margin-top: 50px;">


        <div class="draggable_div rot">
            <div class="rotatable">
                <div id="inner" class="resizable" style="background-color: #3C3C3C; width: 300px; height: 300px;">
                </div>
            </div>
        </div>

  </div>

  <div id="x_and_y_of_inner">Left: , Right: </div>
    $('.draggable_div').draggable();


    $( ".draggable_div" ).draggable({
      drag: function( event, ui ) {

        var left_most_cordinates = $("#outer").offset().left;
        var top_most_cordinates = $("#outer").offset().top;

        $("#x_and_y_of_inner").text( "Left: " + left_most_cordinates + " Top: " + top_most_cordinates );

        ui.position.left = Math.min( left_most_cordinates, ui.position.left );
        ui.position.top = Math.min( top_most_cordinates, ui.position.top );
      }
    });

    $('.resizable').resizable({
        aspectRatio: true,
        handles: 'ne, se, sw, nw'
    });



    $(document).on('mouseover', '.rot', function(){
        var tc = $(this);
        tc.rotatable({
            handle:tc.children('.rotate.left, .rotate.right')
        });
        return true;
    });
.draggable_div
{
    position: relative; 
    display: -moz-inline-stack;
    display: inline-block;
    vertical-align: top;
    zoom: 1;
    *display: inline;   

    cursor: hand; 
    cursor: pointer;       
}

.resizable
{
    width: 50%;   
    border: 1px solid #bb0000;   
}
.resizable img
{
    width: 100%;   
}

.ui-resizable-handle 
{
    background: #f5dc58;
    border: 1px solid #FFF;
    width: 9px;
    height: 9px;

    z-index: 2;
}
.ui-resizable-se
{
    right: -5px;
    bottom: -5px;
}

.ui-rotatable-handle 
{
    background: #f5dc58;
    border: 1px solid #FFF;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -o-border-radius: 5px;
    -webkit-border-radius: 5px;
    cursor: pointer;

    height:        10px;
    left:          50%;
    margin:        0 0 0 -5px;
    position:      absolute;
    top:           -5px;
    width:         10px;
}
.ui-rotatable-handle.ui-draggable-dragging
{
    visibility:  hidden;
}

您需要相应地设置选项
maxWidth
maxHeight
。这两个选项都接受绝对值,因此将值设置为
parent
元素的
width
height

试试这个:

$('.resizable').resizable({
  maxWidth: $('#outer').width(),
  maxHeight: $('#outer').height()
});

使用下面的
可调整大小的
属性,使其可包含在其父级

$('.resizable').resizable({
    aspectRatio: true,
    containment:'#outer', //outer is the id of its root parent
    handles: 'ne, se, sw, nw'
});

您共享的小提琴链接并不能真正阻止盒子跳出黄色盒子。我明白了,伙计。。在我的回答中,我从不使用未经测试的小提琴……)它不允许你的灰色盒子从黄色盒子外面出去,真奇怪。由于某些原因,当我点击它时,它的工作方式不同。我还重新尝试了你在这把小提琴上的建议。没用。你能看看这个对你有用吗?谢谢你的帮助,我现在明白你的意思了。。拖动时不起作用,但仅在向右调整大小时起作用。。您需要将
包容
添加到
可拖动
中,就像对
可调整大小
所做的那样。。但我担心Dragable和Resizeable的组合不会在所有场景中混合使用:)您可能需要为此找到其他解决方法:)哦,明白了。是的,95%都是完美的。由于可拖动和可调整大小的组合,需要找出一种处理奇怪行为的方法