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

前端 未结 9 2235
后悔当初
后悔当初 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: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 = $('
    ', {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}
    
    

    Demo: jsfiddle

提交回复
热议问题