javascript - 使两个文本区域并排共享一个灵活的边框

标签 javascript css flexbox jquery-ui-resizable

我想并排制作两个文本区域,它们共享相同的边框。然后我们可以拖动边框并改变它们的宽度,同时保持它们的宽度总和不变。换句话说,它就像 JSBin 中的面板。

有谁知道如何实现这一点?

PS:这里是并排制作两个固定文本区域的代码:

<!DOCTYPE html>
<html>
<head>
  <title>JS Bin</title>
</head>
<body>
  <textarea name="t1"></textarea>
  <textarea name="t2"></textarea>   
</body>
</html>

编辑1:以下代码来自JSBin的index.html。但我看不出它是如何设法实现共享边界的:

  <div id="panelswaiting">
    <div class="code stretch html panel">
      <div role="menubar" class="label menu" tabindex="0"><span class="name"><strong><a href="#htmlprocessors" role="menuitem" class="fake-dropdown button-dropdown">Processor</a></strong></span><div class="dropdown" id="htmlprocessors">
          <div role="menu" aria-hidden="true" class="dropdownmenu processorSelector" data-type="html">
            <a role="menuitemradio" aria-checked="true" href="#html" data-label="HTML">HTML</a>
            <a role="menuitemradio" href="#markdown">Markdown</a>
            <a role="menuitemradio" href="#jade">Jade</a>
            <a href="#convert">Convert to HTML</a>
          </div>
        </div>
      </div>
      <div class="editbox">
        <textarea aria-label="HTML Code Panel" spellcheck="false" autocapitalize="none" autocorrect="off" id="html"></textarea>
      </div>
    </div>
    <div class="code stretch javascript panel">
      <div role="menubar" class="label menu" tabindex="0"><span class="name"><strong><a role="menuitem" class="fake-dropdown button-dropdown" href="#javascriptprocessors">Processor</a></strong></span>
        <div class="dropdown" id="javascriptprocessors">
          <div role="menu" aria-hidden="true" class="dropdownmenu processorSelector" data-type="javascript">
            <a role="menuitemradio" aria-checked="true" href="#javascript" data-label="JavaScript">JavaScript</a>
            <a role="menuitemradio" href="#babel">ES6 / Babel</a>
            <a role="menuitemradio" href="#jsx">JSX (React)</a>
            <a role="menuitemradio" href="#coffeescript">CoffeeScript</a>
            <a role="menuitemradio" href="#traceur">Traceur</a>
            <a role="menuitemradio" href="#typescript">TypeScript</a>
            <a role="menuitemradio" href="#processing">Processing</a>
            <a role="menuitemradio" href="#livescript">LiveScript</a>
            <a role="menuitemradio" href="#clojurescript">ClojureScript</a>
            <a role="menuitem" href="#convert">Convert to JavaScript</a>
          </div>
        </div>
      </div>
      <div class="editbox">
        <textarea aria-label="JavaScript Code Panel" spellcheck="false" autocapitalize="none" autocorrect="off" id="javascript"></textarea>
      </div>
    </div>
    ...
  </div>

编辑 2:

根据 Ankit vadariya 的回答,我做了 a minimum case ...只剩下一件事了:如何保证.panel-rightmin-width?暂时好像不行。。。

一种方法是设置.panel-leftmax-width,但是如果我们看JSBin,max-width<没有限制 用于每个面板,而有 min-width...

最佳答案

Here is the your solution

希望它对你有用。

代码

$(".panel-left").resizable({
   handleSelector: ".splitter",
   resizeHeight: false
 });

 $(".panel-top").resizable({
   handleSelector: ".splitter-horizontal",
   resizeWidth: false
 });

CSS

html,
body {
  height: 100%;
  font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
  padding: 0;
  margin: 0;
  overflow: auto;
}

.page-container {
  margin: 20px;
}


/* horizontal panel*/

.panel-container {
  display: flex;
  flex-direction: row;
  border: 1px solid silver;
  overflow: hidden;

  /* avoid browser level touch actions */
  xtouch-action: none;
}

.panel-left {
  flex: 0 0 auto;
  /* only manually resize */
  padding: 10px;
  width: 300px;
  min-height: 200px;
  min-width: 150px;
  white-space: nowrap;
  background: #838383;
  color: white;
}

.splitter {
  flex: 0 0 auto;
  width: 18px;  
  background: url(https://raw.githubusercontent.com/RickStrahl/jquery-resizable/master/assets/vsizegrip.png) center center no-repeat #535353;
  min-height: 200px;
  cursor: col-resize;  
}

.panel-right {
  flex: 1 1 auto;
  /* resizable */
  padding: 10px;
  width: 100%;
  min-height: 200px;
  min-width: 200px;
  background: #eee;
}


/* vertical panel */

.panel-container-vertical {
  display: flex;
  flex-direction: column;
  height: 500px;
  border: 1px solid silver;
  overflow: hidden;
}

.panel-top {
  flex: 0 0 auto;
  /* only manually resize */
  padding: 10px;
  height: 150px;
  width: 100%;
  white-space: nowrap;
  background: #838383;
  color: white;
}

.splitter-horizontal {
  flex: 0 0 auto;
  height: 18px;
  background: url(https://raw.githubusercontent.com/RickStrahl/jquery-resizable/master/assets/hsizegrip.png) center center no-repeat #535353;
  cursor: row-resize;
}

.panel-bottom {
  flex: 1 1 auto;
  /* resizable */
  padding: 10px;
  min-height: 200px;
  background: #eee;
}

label {
  font-size: 1.2em;
  display: block;
  font-weight: bold;
  margin: 30px 0 10px;
}

pre {
  margin: 20px;
  padding: 10px;
  background: #eee;
  border: 1px solid silver;
  border-radius: 4px;
  overflow: auto;
}

HTML

<html>
<head>
    <title>Simple Split Panels - jquery-resizable</title>
    <meta charset="utf-8"/>
</head>
<body style="">
    <div class="page-container">

        <h1>
           jquery-resizable - A simple splitter panel
        </h1>
        <hr />

        <p>
            Simple example that demonstrates how to create slidable two-pane layouts <a href="http://caniuse.com/#search=flexbox">using FlexBox</a> and the resizable plug-in.
            Note that Flexbox is not required, but used here to keep the layout simple.
        </p>

        <label>Horizontal Splitter Panes:</label>

        <div class="panel-container">

            <div class="panel-left">
                left panel
            </div>

            <div class="splitter">
            </div>

            <div class="panel-right">
                right panel
            </div>
        </div>

        <label>Vertical Splitter Panes:</label>
        <div class="panel-container-vertical">

            <div class="panel-top">
                top panel
            </div>

            <div class="splitter-horizontal">
            </div>

            <div class="panel-bottom">
                bottom panel
            </div>
        </div>



        <hr />

        <p>
            This example creates two resizables for the horizontal and vertical splitter panes:
        </p>
<pre>
&lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;../src/jquery-resizable.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
    $(&quot;.panel-left&quot;).resizable({
        handleSelector: &quot;.splitter&quot;,
        resizeHeight: false
    });
    $(&quot;.panel-top&quot;).resizable({
        handleSelector: &quot;.splitter-horizontal&quot;,
        resizeWidth: false
    });
&lt;/script&gt;
</pre>
    </div>


</body>
</html>

关于javascript - 使两个文本区域并排共享一个灵活的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41269799/

相关文章:

html - DIV 元素脱离父元素

javascript - 如果使用带有箭头函数的 .find 方法数组为空而不是未定义,如何返回 'null'?

html - 如图所示,创建两个响应式 DIV,它们之间留有空间

html - 悬停后瞬间出现的伪元素

html - 如何在 flexbox 中创建裁剪后的可缩放图像容器?

html - 如何在 Safari 中垂直和水平显示带有 display flex 的 div

javascript - 在 JavaScript 中使用回流隐藏元素,它与 CSS 隐藏属性有何不同?

javascript - 选择angular2中的多个dom元素

javascript - HTML 输入字段宽度(与 JavaScript 相关)

html - 所有元素的 flexbox 大小不一样