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

前端 未结 9 2186
后悔当初
后悔当初 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 17:42

    Here's a slightly different way to do it without float but first with the content changing in real time dynamically ! (This is the best answer)

    var dragging = false;
    
    $('#dragbar').mousedown(function(e){
      e.preventDefault();
      dragging = true;
      var side = $('#side');
      $(document).mousemove(function(ex){
        side.css("width", ex.pageX +2);
      });
    });
    
    $(document).mouseup(function(e){
      if (dragging) 
      {
        $(document).unbind('mousemove');
        dragging = false;
      }
    });
    div{
        color: #fff;
        font-family: Tahoma, Verdana, Segoe, sans-serif;
        
    }
    #container{
        background-color:#2E4272;
        display:flex;
    }
    #side{
        background-color:#4F628E;
        width: 300px;
        position: relative;
    }
    #main{
        background-color:#7887AB;
        z-index: 1;
        flex-grow: 1;
    }
    #dragbar{
       background-color:black;
       height:100%;
       position: absolute;
       top: 0;
       right: 0;
       width: 5px;
       cursor: col-resize;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="container">
        <div id="side">
          Side<br /> Blabla<br /> Blabla<br /> Blabla<br />
          <div id="dragbar"></div>
        </div>
        <div id="main">Dynamically sized content</div>
    </div>

    0 讨论(0)
  • 2020-12-12 17:42

    Use jQuery UI with the ghost option:

    See working jsFiddle demo:

    $( "#resizable" ).resizable({ ghost: true });
    
    0 讨论(0)
  • 2020-12-12 17:44

    Been looking to do this, very nice solution by Gaby. Although my requirement was not to have any absolute elements and use percentages instead of pixels, so I've changed Gaby's code a little to cater for this (if anyone is interested)

    CSS

    #main{
      background-color: BurlyWood;
      float: right;
      height:200px;
      width: 75%;
    }
    #sidebar{
      background-color: IndianRed;
      width:25%;
      float: left;
      height:200px;
      overflow-y: hidden;
    }
    #dragbar{
      background-color:black;
      height:100%;
      float: right;
      width: 3px;
      cursor: col-resize;
    }
    #ghostbar{
      width:3px;
      background-color:#000;
      opacity:0.5;
      position:absolute;
      cursor: col-resize;
      z-index:999
    }
    

    JS

    var i = 0;
    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) 
       {
           var percentage = (e.pageX / window.innerWidth) * 100;
           var mainPercentage = 100-percentage;
    
           $('#console').text("side:" + percentage + " main:" + mainPercentage);
    
           $('#sidebar').css("width",percentage + "%");
           $('#main').css("width",mainPercentage + "%");
           $('#ghostbar').remove();
           $(document).unbind('mousemove');
           dragging = false;
       }
    });
    

    Demo: http://jsfiddle.net/Bek9L/3020/

    0 讨论(0)
  • 2020-12-12 17:52

    Pure JS
    Live Update
    CSS Variable

    let dragging = 0,
      body = document.body,
      target = document.getElementById('dragbar');
    
    function clearJSEvents() {
      dragging = 0;
      body.removeEventListener("mousemove", resize);
      body.classList.remove('resizing');
    }
    
    function resize(e) {
      if (e.pageX > 400 || e.pageX < 200) {
        return;
      }
      body.style.setProperty("--left-width", e.pageX + 'px');
    }
    
    target.onmousedown = function(e) {
      e.preventDefault();
      dragging = 1;
      body.addEventListener('mousemove', resize);
      body.classList.add('resizing');
    };
    
    document.onmouseup = function() {
      dragging ? clearJSEvents() : '';
    };
    body {
      --left-width: 300px;
      padding: 0;
      margin: 0;
    }
    
    body.resizing {
      cursor: col-resize;
    }
    
    #main,
    #sidebar,
    #dragbar {
      top: 0;
      height: 100%;
      background: white;
      position: absolute;
    }
    
    #sidebar {
      background: #e6e9f0;
      width: var(--left-width);
    }
    
    #main {
      right: 0;
      overflow: scroll;
      width: calc(100% - var(--left-width));
    }
    
    #main section {
      margin: 20px auto;
      border-radius: 10px;
      background: white;
      height: 100px;
      width: calc(100% - 60px);
      transition: 0.3s ease-in-out 0s;
      box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.1);
    }
    
    #main section:hover {
      box-shadow: 0px 3px 25px rgba(0, 0, 0, 0.2), 0px 0px 0px 1px #A8D1FD;
    }
    
    #dragbar {
      right: 0;
      width: 5px;
      opacity: 0;
      cursor: col-resize;
      background: #0099e5;
      transition: 0.3s ease-in-out 0s, opacity 0.3s ease-in-out 0s;
    }
    
    #dragbar:hover,
    body.resizing #dragbar {
      opacity: 1;
      transition: 0.3s ease-in-out 0s, opacity 0.3s ease-in-out .3s;
    }
    <body>
      <section id="sidebar">
        <div id="dragbar"></div>
      </section>
      <main id="main">
        <section></section>
        <section></section>
        <section></section>
      </main>
    </body>

    0 讨论(0)
  • 2020-12-12 17:56

    You can find a resizable div here, which provides that feedback but only resizes once you release.

    http://jqueryui.com/demos/resizable/#visual-feedback

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

    I wrote (most of) this a while back http://jsfiddle.net/ydTCZ/12/. It is for a table, but it would not be hard to adapt it to a div. I show you this because it provides insight into the jQuery required to create a resize effect, thus allowing for complete customization to suit your needs.

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