Rohit Verma
Rohit Verma

Reputation: 3783

How to customize resizeable div?

I have a resizeable div and I want it to look like the example in the picture below and in the URL as well:

Resizeable div example

https://bitmax.io/#/margin/usdt/btmx

I want to show the resizeable cursor indicator at the top center of the div instead of the default (bottom right).

My code:-

.third-column-box-2 {
  height: 100px;
  width: 300px;
  resize: vertical;
  background: #ccc;
  overflow: auto;
}

.third-column-box-2 ::-webkit-resizer {
  background-color: red;
}
<div class="third-column-box-2">
  <div class="table-responsive">
    <table class="table table-bordered trade-table mt-0">
      <thead>
        <tr>
          <th class="text-light-purple">Price(USDT)</th>
          <th class="text-light-purple">Size</th>
          <th class="text-light-purple">Time(USDT)</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <div class="text-white"><i class="fa fa-star"></i> ETH/USDT </div>
          </td>
          <td>
            <div class="text-white">411.42</div>
          </td>
          <td>
            <div class="text-red">1181.468</div>
          </td>
        </tr>
        <tr>
          <td>
            <div class="text-white"><i class="fa fa-star"></i> ETH/USDT </div>
          </td>
          <td>
            <div class="text-white">411.42</div>
          </td>
          <td>
            <div class="text-green">1181.468</div>
          </td>
        </tr>
        <tr>
          <td>
            <div class="text-white"><i class="fa fa-star"></i> ETH/USDT </div>
          </td>
          <td>
            <div class="text-white">411.42</div>
          </td>
          <td>
            <div class="text-red">1181.468</div>
          </td>
        </tr>


      </tbody>
    </table>
  </div>
</div>

Upvotes: 0

Views: 88

Answers (1)

user7571182
user7571182

Reputation:

You can use the snippet below to achieve your requirement. Please notice the span tag which makes this div drag-able. You can beautify it a little according to your needs and specifications. I used hiddenbar id to help make the code candid and visual; but you can remove it to achieve the precise result as you want.

Generally the key points to note here are:

Using the pageY property to make it drag-able along NS direction.

A span tag and its center alignment to help get the look and feel as you expected.

The rest code is understandable (if you know JS and jquery, I presume).

var i = 0;
var dragging = false;
   $('#dragbar').mousedown(function(e){
       e.preventDefault();
       
       dragging = true;
       var main = $('#main');
       var wrapper = $('#wrapper');
       var hiddenbar = $('<div>',
                        {id:'hiddenbar',
                         css: {
                                width: main.outerWidth(),
                           			top: e.pageY,
                                left: main.offset().left
                               }
                        }).appendTo('#wrapper');
       
        $(document).mousemove(function(e){
          hiddenbar.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;  
           
           $('#topbar').css("height",percentage + "%");
           $('#main').css("height",mainPercentage + "%");
           $('#hiddenbar').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: #FFB374;
   height:40%;
    width: 100%;
    min-height: 30px;
   max-height: calc(100% - 30px);
}

#topbar{
  display: flex;
  align-items: flex-end;
   background-color: #6380C2;
   width:100%;
   height:60%;
   overflow-y: hidden;
   min-height: 30px;
   max-height: calc(100% - 30px);
}

#dragbar{
   background-color:black;
   color:#fff;
   border-radius: 0 0 4px 4px;
   z-index:2;
   width: 28px;
   position:absolute;
   left: 47%;
   cursor: ns-resize;
   
}
#hiddenbar{
  width: 100%;
    height: 3px;
    background-color:#000;
    opacity:0.5;
    position:absolute;
    cursor: col-resize;
    z-index:999}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="clearfix" id="wrapper">
  <div id="topbar">
       <span id="position"></span>
      <div>
        <span id="dragbar">===</span>
      </div>
  </div>
  <div id="main"> <br/></br>This is One Of the sample div. You have to keep your tables over here.   </div>
</div>

You can also read this amazing blog on various properties and implementation functionalities which I used here for better insights.

Upvotes: 2

Related Questions