How can I resize a DIV by dragging just ONE side of it?

前端 未结 9 2187
后悔当初
后悔当初 2020-12-12 17:38

Let me be more specific... I don\'t want the DIV to resize WHILE I\'m dragging. I want to drag it out (and maybe a vertical line follows my cursor) and when I release, it re

相关标签:
9条回答
  • 2020-12-12 18:06

    There is a much simpler way to achieve this. CSS 3 has a resize property to make an html element resizeable, while following other CSS properties like min/max widths etc.

    .resizeable {
      resize: both;
      overflow: auto;
      border: 2px solid black;
    }
    
    0 讨论(0)
  • 2020-12-12 18:09

    Have a look at this example

    Html

    <div id="sidebar">
         <span id="position"></span>
        <div id="dragbar"></div>
        sidebar
    </div>
    <div id="main">
        main
    </div>
    

    jquery

    var dragging = false;
    $('#dragbar').mousedown(function(e){
       e.preventDefault();
    
       dragging = true;
       var main = $('#main');
       var ghostbar = $('<div>',
                        {id:'ghostbar',
                         css: {
                                height: main.outerHeight(),
                                top: main.offset().top,
                                left: main.offset().left
                               }
                        }).appendTo('body');
    
        $(document).mousemove(function(e){
          ghostbar.css("left",e.pageX+2);
       });
    });
    
    $(document).mouseup(function(e){
       if (dragging) 
       {
           $('#sidebar').css("width",e.pageX+2);
           $('#main').css("left",e.pageX+2);
           $('#ghostbar').remove();
           $(document).unbind('mousemove');
           dragging = false;
       }
     });
    

    Demo at http://jsfiddle.net/gaby/Bek9L/1779/

    it is an alteration from the code i posted in Emulating frame-resize behavior with divs using jQuery without using jQuery UI?

    0 讨论(0)
  • 2020-12-12 18:09

    I edited solution from the 1st comment but for vertical resizing of blocks

    var i = 0;
    var dragging = false;
       $('#dragbar').mousedown(function(e){
           e.preventDefault();
           
           dragging = true;
           var main = $('#main');
           var wrapper = $('#wrapper');
           var ghostbar = $('<div>',
                            {id:'ghostbar',
                             css: {
                                    width: main.outerWidth(),
                               			top: e.pageY,
                                    left: main.offset().left
                                   }
                            }).appendTo('#wrapper');
           
            $(document).mousemove(function(e){
              ghostbar.css("top", (e.pageY + 2));
           });
           
        });
    
       $(document).mouseup(function(e){
           if (dragging) 
           {
               var percentage = ((e.pageY - $('#wrapper').offset().top) / $('#wrapper').height()) * 100;
               var mainPercentage = 100-percentage;  
               
               $('#sidebar').css("height",percentage + "%");
               $('#main').css("height",mainPercentage + "%");
               $('#ghostbar').remove();
               $(document).unbind('mousemove');
               dragging = false;
           }
        });
    body,html{width:100%;height:100%;padding:0;margin:0;}
    .clearfix:after {
        content: '';
        display: table;
        clear: both;
    }
    #wrapper {
      width: 600px;
      margin: 50px auto 0 auto;
      height: 300px;
      background: yellow;
    }
    
    #main{
       background-color: BurlyWood;
       height:40%;
        width: 100%;
        min-height: 30px;
       max-height: calc(100% - 30px);
    }
    #sidebar{
      display: flex;
      align-items: flex-end;
       background-color: IndianRed;
       width:100%;
       height:60%;
       overflow-y: hidden;
       min-height: 30px;
       max-height: calc(100% - 30px);
    }
    
    #dragbar{
       background-color:black;
       height:3px;
       float: right;
       width: 100%;
       cursor: row-resize;
    }
    #ghostbar{
      width: 100%;
        height: 3px;
        background-color:#000;
        opacity:0.5;
        position:absolute;
        cursor: col-resize;
        z-index:999}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
    <div class="clearfix" id="wrapper">
      <div id="sidebar">
           <span id="position"></span>
          <div id="dragbar"></div>
      </div>
      <div id="main">  </div>
    </div>

    Demo: jsfiddle

    0 讨论(0)
提交回复
热议问题