【问题标题】:How to make this resizable div, resizable from edges?如何使这个可调整大小的 div 可以从边缘调整大小?
【发布时间】:2016-01-05 04:30:01
【问题描述】:

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

目前只能通过从角落拉伸来调整框的大小。我也希望能够从边缘调整它的大小。 从角落拉伸使其在宽度和高度上均匀缩放。但是,在一条边上缩放,只会沿长度缩放。而在另一条边上缩放它,只会沿宽度缩放它。 我如何做到这一点?

我的代码在这里。 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;
}

【问题讨论】:

    标签: javascript jquery html resize


    【解决方案1】:

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

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

    DEMO

    【讨论】:

    • 知道了。这很棒。但是如何让可调整大小的光标显示在悬停在边缘上?
    • 您需要添加更多样式来为所有边指定cursor 并处理它们。检查此DEMO for 2 sides - right and top
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      做起来很简单,不需要用 JavaScript 让自己复杂化。通常越简单,它就越能保证它适用于所有浏览器。 有时“少即是多” 只需在你的 CSS 中使用它,它就可以工作了。

      .divName {
          resize: both;
          overflow: auto;
      } 
      

      您也可以使用resize:horizontal;resize:vertical;resize:none;

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

      .divName {
          resize: vertical;
          overflow: auto;
      } 
      

      【讨论】:

      【解决方案4】:

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

      div
      {
          resize: both;
          overflow: auto;
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-28
      • 2023-03-25
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多