如何制作这个可调整大小的div,可以从边缘调整大小?

时间:2016-01-05 04:30:02

标签: javascript jquery html resize

我有一个可调整大小且可拖动的盒子(灰色)。可以通过从角落拉伸盒子来调整盒子的大小。

目前只能通过从角落拉伸来调整框的大小。我希望能够从边缘调整大小。 从角落拉伸使其在宽度和高度上均匀地伸缩。但是,缩放一条边,只会沿长度缩放。同时,将其缩放到另一个边缘,将仅沿宽度缩放。 我如何实现这一目标?

我的代码在这里。 http://jsfiddle.net/akashdmukherjee/sa44ks9u/4/

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>

JS:

    $('.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;
    });

CSS:

.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;
}

4 个答案:

答案 0 :(得分:6)

如果要保留宽高比,则无法实现此目的。因此,基本上当您从resizable中删除该选项时,您可以根据需要使用角在任何方向上拖动,并在高度和宽度上进行缩放。

$('.resizable').resizable({
    //aspectRatio: true, //comment or remove this
    handles: 'ne, se, sw, nw'
});

<强> DEMO

答案 1 :(得分:1)

试试这段代码,在这段代码中我从边缘调整大小:

HTML

<div id='resizeDiv'><div id='handle' class="ui-resizable-handle ui-resizable-s"></div>
</div>

CSS

#resizeDiv {
    margin: 20px;
    width: 100px;
    height: 100px;
    border: 1px solid #CCC;
    background-color: #FAFAFA;

}

#handle {
    width: 100px;
    height: 10px;
    background-color: gray;
}

JS

$(function() {
        $( "#resizeDiv" ).resizable({handles: {'s': '#handle'}});
    });

JSfiddle click here...

答案 2 :(得分:0)

操作简单,无需使用JavaScript复杂化。通常越简单越保证它将适用于所有浏览器。 有时“少即是多” 只需在你的CSS和wallah中使用它就可以了。

Rectangle

您还可以使用Square.divName { resize: both; overflow: auto; } resize:horizontal;

如示例中所示,如果您只想要垂直调整大小而不是水平调整:

resize:vertical;

答案 3 :(得分:0)

只需使用css属性调整大小溢出:

div
{
    resize: both;
    overflow: auto;
}
相关问题