javascript - 两个区域的垂直可拖动划分

标签 javascript jquery html css draggable

我想对两个区域进行垂直拖动划分,如下所示。 enter image description here enter image description here

我只想修改一个online example可拖动的 div 是我想要的。最后,我得到了this .有人可以给我一些修改提示吗?


JSFiddle 链接:https://jsfiddle.net/casperhongkong/omekvtka/14/


HTML

<div class="container">
  <div class="area1">
Area 1
  </div>
  <div class="drag">

  </div>
  <div class="area2">
Area 2
  </div>
</div>

CSS

.container {
  position: fixed;
  top: 51px;
  right: 0px;
  bottom: 0px;
  left: 0px;
  background-color: #272822;
  border: 1px solid #222;
 // margin: 0 auto;
  //display: inline-block;
}

.area1 {
  position: absolute;
  height: 100%;
  width: 30%;
  background-color: #ddd;
  display: inline-block;
}

.drag {
  position: fixed;

  width: 5px;
  height: 100%;
  background-color: #444;
  display: inline-block;
}

.area2 {
  position: absolute;
  right: 0;
  height: 100%;
  width: 30%;
  background-color: #ddd;
  display: inline-block;
}

JavaScript

$(document).ready(function() {

  $('.drag').on('mousedown', function(e) {
    var $area1 = $('.area1'),
        $area2 = $('.area2'),
        startWidth_a1 = $area1.width(),
        startWidth_a2 = $area2.width(),
        pX = e.pageX;

    $(document).on('mouseup', function(e) {
      $(document).off('mouseup').off('mousemove');
    });

    $(document).on('mousemove', function(me) {
      var mx = (me.pageX - pX);
      $area1.css({
        width: startWidth_a1 - mx;
      });
      $area2.css({
        //left: mx / 2,
        width: startWidth_a2 - mx,
        //top: my
      });
    });

  });
});

最佳答案

对于 javascript,我建议检查一个库,因为这比仅仅几行要复杂得多。 @fauxserious 以 Split.js 为例。

这在纯 HTML/CSS 中是可能的,尽管有一些限制,如讨论的那样 here .

HTML:

<div class="split-view">
    <div class="resize-x panel" style="width: 216px;">
      Panel A
    </div>
    <div class="panel">
      Panel B
    </div>
</div>

CSS:

/* Panels: */
.panel{ 
    padding: 1em; 
    border-width: 6px; 
    border-style: solid; 
    height: 4em; 
}

/* Resizing */
.resize-x { 
    resize: horizontal;
    overflow: auto;
}

/* Split View */
.split-view {
    margin: 1em 0; 
    width: 100%;
    clear: both;
    display: table;
}

.split-view .panel {
    display: table-cell;
}

关于javascript - 两个区域的垂直可拖动划分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472704/

相关文章:

javascript - 如何使用 FireLESS 的开发输出来编译 lessc 或 escape 的 Node 版本

javascript - React-native 启动画面和 react-navigation

javascript - 为动态内容和不同的嵌套结构堆叠嵌套的 Div,如 Accordion (无插件)

javascript - 隐藏第一个面板上的导航 - PanelSnap 插件

html - CSS Grid 将剩余的可用空间用于之间的所有元素

javascript - window.addEventListener ("load",function() 是否比放置在页面底部的 &lt;script&gt; 更快?

javascript - 为什么 (!+[]+[]) 在 Javascript 中是 'true' 而 (false + []) 是 'false'?

javascript - Jquery Mobile - 如何在 pagebeforechange 事件中获取数据 url

jquery - 根据元素大小部分工作

javascript - 多词 Google 搜索未返回结果