简体   繁体   English

同时调整多个元素的大小

[英]Resize several elements simultaneously

I am trying create a "textarea" with a toolbar, to display rich text content based on the html content stored on a textarea in the backgroud.我正在尝试使用工具栏创建一个“textarea”,以显示基于存储在背景中 textarea 上的 html 内容的富文本内容。 the html is just like that: html 就是这样:

    <div class="primary-content">
        <div class="textarea-with-nav" id="outerWrap">
            <div class="navbar">
                <ul>
                    <li><a href="#">teste1</a></li>
                    <li><a href="#">teste2</a></li>
                    <li><a href="#">teste3</a></li>
                </ul>
            </div>
            <textarea id="layer1"></textarea>
        <textarea id="layer2"></textarea>
        </div>
    </div>

with the css style right now looks like that: css 样式现在看起来像这样:

html, body {
    margin: 0;
    height: 100%;
}
.primary-content {
    padding-top: 55px;
}
.navbar {
    grid-area: header;
    overflow: hidden;
    background-color: #333;
    width: 100%;
        z-index: 1;
}
.navbar.site-nav {
    position: fixed;
    top: 0;
}
.navbar ul { list-style-type: none; }
.navbar ul li { display: inline; }
.navbar ul li a {
    color: white;
    padding: 8px 16px;
    text-decoration: none;
}
.navbar ul li a:hover {
    background-color: #555;
    color: white;
    height: 100%;
}
.textarea-with-nav {
        transform: translate(50%);
    width: 400px;
    height: 300px;
    position: realtive;
    border-radius: 5px;
    border: 1px solid #ccc;
}
.textarea-with-nav > .navbar {
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
}
.textarea-with-nav > textarea {
    border: none;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    width: 396px !important;
    height: 246px !important;
    resize: none;
}
.textarea-with-nav > textarea:focus { outline: none; }

#outerWrap {
position: relative;
z-index: 0;
}

#layer1 {
position: absolute;
z-index: 1;
}

#layer2 {
position: absolute;
z-index: 2;
}

the two textareas should be resized simultaneously when layer2 (the one in the front) is resized (and layer1 maybe would not be a textarea in the final version of this).当调整layer2 (前面的那个)的大小时,应该同时调整两个 textarea 的大小(并且layer1在最终版本中可能不是 textarea)。

also, if I resize the textareas horizontally, the navbar should stretch too, to keep track of the textarea new dimension.此外,如果我水平调整 textarea 的大小,导航栏也应该拉伸,以跟踪 textarea 的新维度。

Anyone knows how to make this happen?任何人都知道如何做到这一点? Does this requires some javascript code, or only css should suffice?这是否需要一些 javascript 代码,还是只需要 css 就足够了?

Your end goal, as I understand it is:据我了解,您的最终目标是:

  • Create a rich textbox editor.创建富文本框编辑器。
    • With two textarea elements有两个textarea元素
    • With one tool-bar .用一个tool-bar
      • We will no longer assume this is actually a nav-bar .我们将不再假设这实际上是一个nav-bar

The requirements in your post aren't 100% clear, but as I understand them:您帖子中的要求不是 100% 明确,但据我了解:

When one textarea resizes, the other does to, along with the toolbar .当一个textarea调整大小时,另一个与toolbar一起调整大小。

My only assumption, is based on a comment you made on another answer:我唯一的假设是基于您对另一个答案的评论:

With this, the textareas are shown side by side, when I want one on top of the other...有了这个,文本区域并排显示,当我想要一个在另一个之上时......

I am going to assume that only adjusting the width of the textarea impacts the other not the height.我将假设仅调整textarea的宽度会影响另一个而不是高度。


Below is a minimal pure CSS example that'll get you headed in the right direction.下面是一个最小的纯 CSS 示例,它将让您朝着正确的方向前进。 It's buggy (working with textarea always seems to be when it comes to sizing), but since this question is almost verbatim, my answer to your previous question , I'll let you work out the kinks in it.它有问题(在调整大小时似乎总是使用textarea ),但由于这个问题几乎是逐字逐句的,我对你之前问题的回答,我会让你解决其中的问题。 I highly recommend taking a look at the CSS calc function .我强烈建议您查看CSS calc function

 /* Rich Text Editor */.rich-text-editor { display: flex; align-items: center; justify-content: center; flex-direction: column; min-width: 300px; background-color: #ccca; padding: 15px; }.rich-text-editor >.toolbar { list-style-type: none; width: 100%; padding: 0; margin: 0; background: #f33; background: -webkit-linear-gradient(to right, #f33, #f77); background: linear-gradient(to right, #f33, #f77); padding: 5px 6px; }.rich-text-editor >.toolbar > li { display: inline-block; color: #fff; margin: 0 5px; padding: 5px; width: 15px; border: 1px solid #fff5; text-align: center; cursor: pointer; user-select: none; border-radius: 5px; transition: 0.3s linear all; }.rich-text-editor >.toolbar > li:hover { background-color: #fff5; }.rich-text-editor > textarea { width: 100%; border: 1px solid #ccc; border-top: none; padding: 5px; } /* Your Navbar */.navbar { grid-area: header; overflow: hidden; background-color: #333; width: 100%; z-index: 1; }.navbar.site-nav { position: fixed; top: 0; }.navbar ul { list-style-type: none; }.navbar ul li { display: inline; }.navbar ul li a { color: white; padding: 8px 16px; text-decoration: none; }.navbar ul li a:hover { background-color: #555; color: white; height: 100%; } /* Just for Demonstration */ html, body { margin: 0; height: 100%; }.primary-content { height: calc(100% - 50px); display: flex; align-items: center; justify-content: center; }
 <div class="navbar site-nav"> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Edit</a></li> </ul> </div> <div class="primary-content"> <div id="rte-test" class="rich-text-editor"> <ul class="toolbar"> <li>H</li> <li>=</li> <li>/</li> <li>_</li> </ul> <textarea></textarea> <textarea></textarea> </div> </div>

There is also the option of using JavaScript to manipulate the textarea s respectfully, but it's a bit over the top and has the same issue that the pure CSS version does:还可以选择使用JavaScript来恭敬地操作textarea ,但它有点过头了,并且与纯 CSS 版本存在相同的问题:

 var richTextEditor = document.getElementById("rte-test"); var oldWidth = 0; function resize(index, altIndex) { var textAreas = richTextEditor.getElementsByTagName("TEXTAREA"); var width = textAreas[index].clientWidth; if (oldWidth.== width) { textAreas[altIndex].style;width = width + "px"; oldWidth = width; } }
 .rich-text-editor { display: flex; align-items: center; justify-content: center; flex-direction: column; }
 <div id="rte-test" class="rich-text-editor"> <textarea onmouseup="resize(0, 1);"></textarea> <textarea onmouseup="resize(1, 0);"></textarea> </div>

I wish you the best of luck in your future endeavors.我祝你在未来的努力中好运。

All your textarea have position: absolute , so I think it did no work.你所有的 textarea 都有position: absolute ,所以我认为它没有用。

Try this with flex :试试这个flex

 .primary-content { position: relative; z-index: 0; height: 480px; width: 640px; }.navbar { grid-area: header; overflow: hidden; background-color: #333; width: 100%; z-index: 1; }.navbar.site-nav { position: fixed; top: 0; }.navbar ul { list-style-type: none; }.navbar ul li { display: inline; }.navbar ul li a { color: white; padding: 8px 16px; text-decoration: none; }.navbar ul li a:hover { background-color: #555; color: white; height: 100%; }.textarea-with-nav { transform: translate(50%); width: 400px; height: 300px; position: relative; border-radius: 5px; border: 1px solid #ccc; }.textarea-with-nav>.navbar { border-top-left-radius: 5px; border-top-right-radius: 5px; }.textarea-with-nav>textarea { border: none; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; width: 100%; height: 100%; resize: both; } #layer1 { z-index: 1; height: 100%; } #layer2 { z-index: 2; height: 100%; }.flex{ display: flex; flex-direction: row; justify-content: space-between; }.insideTextArea{ width: "50%" }
 <div class="textarea-with-nav" id="outerWrap"> <div class="navbar"> <ul> <li><a href="#">teste1</a></li> <li><a href="#">teste2</a></li> <li><a href="#">teste3</a></li> </ul> </div> <div class="flex"> <textarea id="layer1" class="insideTextArea"></textarea> <textarea id="layer2" class="insideTextArea"></textarea> </div> </div>

I don't test, but something like this can do the job.我不测试,但这样的事情可以完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM